我有以下代码:
class A : public B {
public:
_container (B* b) {
container_ = b;
}
private:
B* container_;
};
void foo(const A& a, const B& b) {
A new_a (a);
new_a._container(&b);
}
如果我尝试使用icpc12编译它,我会得到:-
error: no instance of overloaded function "A::_container
我从许多其他堆栈溢出线程(如)中了解到,模板参数是在编译时计算的。此外,非类型模板参数应该是一个常量表达式、积分表达式或指向具有外部链接的对象的指针。
而且,我在Makefile中的g++命令中没有使用--std=c++0x。
那么,是否可以实例化作为参数传递为NULL的模板类?
// I have a template class like this -
template<class T, T invalidVal, int e> class A
{
static inline bool dummy(T value)
{
return 0;
为什么会发生错误,我认为const auto data_0应该和const Data一样* data_1,data_0和data_1有什么不同?
class Data {
public:
Data(int val_) : val(val_) {
}
~Data() {
}
void SetVal(int val_) {
val = val_;
}
private:
int val;
};
Data* GetData(int val) {
return new Data(val);
}
int main () {
const auto data_0
这是的后续
事情变得非常复杂,所以我开始了一个新的帖子,以使我的观点更清晰。(我不想删除以前的帖子,因为其他给出有价值反馈的人不会失去他们获得的名誉点)
更新代码:(编译并运行)
#include <iostream>
using std::cout;
class Test {
public:
Test(){ }
int foo (const int) const;
int foo (int );
};
int main ()
{
Test obj;
我正在阅读C++ Primer,我发现了一些非常奇怪和难以理解的东西:
Record lookup(Account&); //Record and Account are two unrelated class
Record lookup(const Account&);
因此,这两个函数都接受非const对象(首先匹配非const参数函数),但只有第二个函数可以接受const对象。
从下一节(const_cast和重载)开始,我有以下代码:
const string &shorterString(const string &s1, const string
我有以下代码(为了简短起见,只显示了相关部分-如果我太简短了,请告诉我):
class my_class
{
public:
my_class() {m_i=0;}
set(int i) {m_i = i;}
private:
int m_i;
}
void CallMod()
{
// create a bunch of my_class* o = new my_class() and store in vector<my_class*>
// vObject (left out for brevity)
Mod
我试图为我的结构指针分配内存,我是全局声明的。但是我得到了这个错误-> error: initializer element is not constant
typedef struct A {
uint32_t arr[30][4096];
uint32_t var1;
uint8_t var2;
bool var3;
}B;
B *x = (B*)malloc(sizeof(B));
有人能解释我在哪里做错了吗。另外,与动态内存分配相比,是否有一种将内存分配给结构指针的直接方法?提前谢谢。
我尝试了,所以它让我思考为什么不重载这个函数,我想出了一个稍微不同的代码,但它说这个函数不能重载。我的问题是为什么?还是有别的办法?
#include <iostream>
using std::cout;
class Test {
public:
Test(){ }
int foo (const int) const;
int foo (int );
};
int main ()
{
Test obj;
Test const obj1;
i
以下是从C++ Vol1第10章中的思考中复制的一些代码。
#include <iostream>
using namespace std;
int x = 100;
class WithStatic {
static int x;
static int y;
public:
void print() const {
cout << "WithStatic::x = " << x << endl;
以下程序编译:
template <const int * P>
class Test{};
extern const int var = 42; //extern needed to force external linkage
int main()
{
Test<&var> test;
}
然而,这一次却没有,这对我来说是个惊喜:
template <const int * P>
class Test{};
extern const int var = 42; //extern needed to force external li
我正在尝试用它的构造函数初始化我的类ShadeRec的一个实例:
ShadeRec(World& world);
所以我传递给它:
ShadeRec sr(*this);
其中"this“是类World的实例。
我得到以下错误:
World.cpp: In member function ‘ShadeRec World::hitObjects(const Ray&) const’:
World.cpp:52: error: no matching function for call to ‘ShadeRec::ShadeRec(const World&)’
Sh
我在编译我的代码时遇到了一些问题--这与我传入的类型有关。下面是编译器所说的:
R3Mesh.cpp: In copy constructor 'R3Mesh::R3Mesh(const R3Mesh&)':
R3Mesh.cpp:79: error: no matching function for call to 'R3Mesh::CreateHalfEdge(R3MeshVertex*&, R3MeshFace*&, R3MeshHalfEdge*&, R3MeshHalfEdge*&)'
R3Mesh.h:178:
class myClass {
int arr[100];
public:
void *get(long i, void* const to) const;
void *get(long i, bool nog);
void *tstfn(void* const to) { return get(0L,to); }
};
-Wall gcc说:
dt.cpp: In member function ‘void* myClass::tstfn(void*)’:
dt.cpp:6:49: warning: ISO C++ says that these are amb
我的问题是我得到了这个错误
binarytree.cpp: In member function ‘void BinaryTree<T>::printPaths(const BinaryTree<T>::Node*) const [with T = int]’:
binarytree.cpp:88: instantiated from ‘void BinaryTree<T>::printPaths() const [with T = int]’
main.cpp:113: instantiated from ‘void printTreeInfo(c