我是C++初學者。我已經學會了如何定義派生類的構造函數的類成員:如何定義派生類的非成員構造函數(在類頭文件中)
class A{
...
public:
A(params){}
};
class B :public A{
...
public:
B(param1OfA, param2OfA, params) :A(param1OfA, param2OfA){}
};
現在,我認爲同樣的原則將派生類的構造函數的非成員定義以及工作:
class A{
...
public:
A(params);
}
A::A(params){};
class B :public A{
...
public:
B(param1OfA, param2OfA, params) :A(param1OfA, param2OfA);
};
B::B(param1OfA, param2OfA, params) :A(param1OfA, param2OfA){}
而是,我得到這個錯誤在Visual Studio:
1> Source.cpp
1>d:\webdev\c++\godina ii - parcijala-i\aa-vjezba-polimorfizam\source.cpp(63): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>d:\webdev\c++\godina ii - parcijala-i\aa-vjezba-polimorfizam\source.cpp(67): error C2144: syntax error : 'std::string' should be preceded by ')'
1>d:\webdev\c++\godina ii - parcijala-i\aa-vjezba-polimorfizam\source.cpp(67): error C2630: ';' found in what should be a comma-separated list
1>d:\webdev\c++\godina ii - parcijala-i\aa-vjezba-polimorfizam\source.cpp(67): error C2612: trailing 'type' illegal in base/member initializer list
1>d:\webdev\c++\godina ii - parcijala-i\aa-vjezba-polimorfizam\source.cpp(84): fatal error C1004: unexpected end-of-file found
你爲什麼定義構造函數兩次? –
@πάνταῥεῖ如果您在'public'部分中引用構造函數聲明後的第二個示例(非成員)和花括號的用法,那些複製第一個示例的拼寫錯誤。我已經刪除了這些。 – developer10