我有一个带有两个构造函数的类,一个不带参数,另一个带一个参数.
使用带有一个参数的构造函数创建对象可以按预期工作.但是,如果我使用不带参数的构造函数创建对象,我会收到错误.
例如,如果我编译此代码(使用g ++ 4.0.1)...
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2();
foo2.bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
...我收到以下错误:
nonclass.cpp: In function ‘int main(int, const char**)’:
nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’
Run Code Online (Sandbox Code Playgroud)
为什么这样,我如何使它工作?
嘿所有,所以我试图构建一个简单的二叉树,它有两个键,并评估其排序的总和.这是它的样子:
struct SumNode
{
int keyA;
int keyB;
SumNode *left;
SumNode *right;
};
class SumBTree
{
public:
SumBTree();
~SumBTree();
void insert(int, int);
SumNode *search(int, int);
SumNode *search(int);
void destroy_tree();
private:
SumNode *root;
void insert(int,int, SumNode*);
SumNode *search(int,int, SumNode*);
SumNode *search(int, SumNode*);
void destroy_tree(SumNode*);
};
SumBTree::SumBTree()
{
root = NULL;
}
SumBTree::~SumBTree(){};
void SumBTree::insert(int a, int b, SumNode *leaf)
{
int sum = a + b;
int leafsum = leaf->keyA + leaf->keyB;
if (sum < leafsum)
{
if (leaf->left != NULL) …Run Code Online (Sandbox Code Playgroud)