如何修复预期的primary-expression?

时间:2016-12-27 16:28:27

标签: c++ smart-pointers

#include<iostream>
#include<memory>

class test{

    public:
        void print()
        {
            std::cout<<"test print"<<std::endl;
        }

};

int main
{
    std::auto_ptr<test> t1 (new test);
    t1->print();

    return 0;
}

我收到以下错误:

$g++ 5.cpp --std=c++11
5.cpp:16:22: error: expected primary-expression before ‘t1’
  std::auto_ptr<test> t1 (new test);
                      ^
5.cpp:16:22: error: expected ‘}’ before ‘t1’
5.cpp:16:22: error: expected ‘,’ or ‘;’ before ‘t1’
5.cpp:17:2: error: ‘t1’ does not name a type
  t1->print();
  ^
5.cpp:19:2: error: expected unqualified-id before ‘return’
  return 0;
  ^
5.cpp:20:1: error: expected declaration before ‘}’ token
 }
 ^

2 个答案:

答案 0 :(得分:4)

int main // <-- Notice anything ?

答案 1 :(得分:3)

问题是,你忘记了主要的parantheses。

int main { ...} // this is wrong!

但右边是

int main() { ... }