C++ 错误:请求成员'';在';中';它是非类类型的

C++ 错误:请求成员'';在';中';它是非类类型的,c++,C++,我有一个有两个构造函数的类,一个不带参数,一个带一个参数 使用接受一个参数的构造函数创建对象的效果与预期一样。但是,如果使用不带参数的构造函数创建对象,则会出现错误 例如,如果我编译这段代码(使用g++4.0.1) 。。。我得到以下错误: nonclass.cpp: In function ‘int main(int, const char**)’: nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of n

我有一个有两个构造函数的类,一个不带参数,一个带一个参数

使用接受一个参数的构造函数创建对象的效果与预期一样。但是,如果使用不带参数的构造函数创建对象,则会出现错误

例如,如果我编译这段代码(使用g++4.0.1)

。。。我得到以下错误:

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();
改为

Foo foo2;
您得到错误是因为编译器认为

Foo foo2()
从名为“foo2”且返回类型为“Foo”的函数声明开始

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

,只是为了记录

这实际上不是代码的解决方案,但当错误地访问由
myPointerToClass
指向的类实例的方法时,我收到了相同的错误消息,例如

MyClass* myPointerToClass = new MyClass();
myPointerToClass.aMethodOfThatClass();
在哪里


显然是正确的。

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

if(class_iter->num == *int_iter)
即使IDE给了我正确的类成员。显然,问题在于
“anything”::迭代器
没有名为
num
的成员,因此我需要取消对它的引用。但不是这样的:

if(*class_iter->num == *int_iter)
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;
}
…显然。我最终解决了这个问题:

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

我希望这有助于像我这样遇到这个问题的人。

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

if(*class_iter->num == *int_iter)
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;
}

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

只需使用Foo-foo2


它会工作的。

我遇到了一个案例,在这个案例中,我收到了那个错误消息,并且

Foo foo(Bar());
基本上是试图将一个临时Bar对象传递给Foo构造函数。结果发现编译器正在将其转换为

Foo foo(Bar(*)());

也就是说,一个名为foo的函数声明返回一个接受参数的foo——一个返回带有0个参数的条的函数指针。当像这样传递临时值时,最好使用
Bar{}
而不是
Bar()
来消除歧义。

如果要声明没有参数的新物质(知道对象有默认参数),请不要写入

 type substance1();
但是


当然,这是一个极端的错误,但我在另一种情况下收到它,当我试图重载赋值
操作符=
时。在我看来,这有点神秘(来自g++8.1.1)

(叮当声的等效误差为:
错误:成员引用基类型“float”不是结构或联合


对于行
data.i=数据
数据。f=数据。原来编译器混淆了局部变量名“data”和我的成员变量
data
。当我将其更改为
void操作符=(T newData)
data.I=newData
data.f=newData,错误消失了。

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

C++11已经实现了。利用这个我们可以做类似的事情

Temp t{String()};
然而,这:

Temp t(String());
抛出编译错误,因为它认为
t
属于
Temp(String(*)()
)类型

#包括
类字符串{
公众:
字符串(常量字符*str):ptr(str)
{

std::cout related:这是相同的答案,因为标准要求编译器将任何可能是函数声明的内容解释为函数声明,以防止歧义。(具体地说,在和之间,标准明确规定,在出现歧义的情况下,任何可以解释为声明的东西都是声明,以解决这种歧义。)我不理解为什么编译器会这样想:Foo foo2()从函数声明开始,在主函数中。显然,我之前已经得到了这个答案,因为我无法再次向上投票!这里是文本第二次向上投票…第二次感谢!无参数函数声明应该强制执行“void”参数,以便从一致性的角度来看允许使用此用法。如果我没有弄错的话,K&R C强制使用术语void。@Rajesh:void参数列表不是强制的,它只是表示与空参数列表(未指定参数)不同的内容(零参数).这是切换到c++11中引入的另一个很好的原因
error: request for member ‘i’ [and 'f'] in ‘data’, which is of non-class type ‘float’
Temp t{String()};
Temp t(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;
}