C ++中没有可用的成员

时间:2013-06-05 19:32:15

标签: c++ visual-studio-2010 most-vexing-parse

我有一个班级

class Test{
public:
    Test(){};
    ~Test(){};
    void test() {cout<<"test"<<endl;};
};

在main.cpp中我有:

#include "Test.h"

using namespace std;

int main(){
     Test t();
     t.test();
}

这是宣告方法的正确方法还是我弄错了? VS2010根本不识别这种方法。它说明了

  

表达式必须具有类类型

2 个答案:

答案 0 :(得分:1)

你在这里声明一个功能:

Test t(); // function t(), returning a Test instance

请改为尝试:

Test t;  // t is a Test instance
Test t2{}; // t2 is a Test instance, C++11 only

答案 1 :(得分:0)

First thing is that 
class Test{
public:
    Test(){};(Your Default Constructor when you will make the object this constructor will call)
    ~Test(){};(when you will release this object your destructor will call)
    void test() {cout<<"test"<<endl;};
};
Here you don't need to call manually.