我有一个有两个构造函数的类,一个不接受参数,一个接受一个参数。

使用带一个参数的构造函数创建对象可以正常工作。但是,如果我使用不带参数的构造函数创建对象,就会得到一个错误。

例如,如果我编译这段代码(使用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;
}

... 我得到以下错误:

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 ()()’

为什么会这样,我该怎么做呢?


当前回答

当不打算使用参数化构造函数时,实例化类对象不需要括号。

只需使用Foo foo2;

它会起作用的。

其他回答

我有一个类似的错误,似乎编译器误解了没有参数的构造函数的调用。我通过从变量声明中删除圆括号来使它工作,在你的代码中是这样的:

class Foo
{
  public:
    Foo() {};
    Foo(int a) {};
    void bar() {};
};

int main()
{
  // this works...
  Foo foo1(1);
  foo1.bar();

  // this does not...
  Foo foo2; // Without "()" 
  foo2.bar();

  return 0;
}

只是为了记录…

它实际上不是一个解决方案,你的代码,但我有相同的错误消息时,不正确地访问一个类实例由myPointerToClass指向的方法,例如。

MyClass* myPointerToClass = new MyClass();
myPointerToClass.aMethodOfThatClass();

在哪里

myPointerToClass->aMethodOfThatClass();

显然是正确的。

Foo foo2();

改变

Foo foo2;

你得到错误,因为编译器认为

Foo foo2()

如函数声明名称为'foo2',返回类型为'Foo'。

但在这种情况下,如果我们更改为Foo foo2,编译器可能会显示错误“重载' Foo() '的调用是模糊的”。

@MykolaGolubyev已经给出了精彩的解释。我正在寻找一个解决方案来做这样的MyClass obj (MyAnotherClass()),但编译器将其解释为函数声明。

c++ 11有括号-init-list。用这个我们可以做这样的事情

Temp t{String()};

然而,这种:

Temp t(String());

当它认为t的类型为Temp(String(*)())时,抛出编译错误。

#include <iostream>

class String {
public:
    String(const char* str): ptr(str)
    {
        std::cout << "Constructor: " << str << std::endl;
    }
    String(void): ptr(nullptr)
    {
        std::cout << "Constructor" << std::endl;
    }
    virtual ~String(void)
    {
        std::cout << "Destructor" << std::endl;
    }

private:
    const char *ptr;
};

class Temp {
public:
    Temp(String in): str(in)
    {
        std::cout << "Temp Constructor" << std::endl;
    }

    Temp(): str(String("hello"))
    {
        std::cout << "Temp Constructor: 2" << std::endl;
    }
    virtual ~Temp(void)
    {
        std::cout << "Temp Destructor" << std::endl;
    }

    virtual String get_str()
    {
        return str;
    }

private:
    String str;
};

int main(void)
{
    Temp t{String()}; // Compiles Success!
    // Temp t(String()); // Doesn't compile. Considers "t" as of type: Temp(String (*)())
    t.get_str(); // dummy statement just to check if we are able to access the member
    return 0;
}

添加到知识库中,我得到了相同的错误

if(class_iter->num == *int_iter)

尽管IDE给了我class_iter的正确成员。显然,问题是"anything"::iterator没有一个名为num的成员,所以我需要对它解引用。它不是这样工作的:

if(*class_iter->num == *int_iter)

…很明显。我最终用这个解决了这个问题:

if((*class_iter)->num == *int_iter)

我希望这能帮助到像我一样遇到这个问题的人。