你好,我知道,要聲明一個std::vector
我必須做這樣的如何在hpp中聲明一個向量?
std::vector<double> a(0);
但在我的文件,這是行不通的。這裏是我的代碼:
main.cpp中:
#include "test.hpp"
int main()
{
Test test;
return EXIT_SUCCESS;
}
test.hpp:
#ifndef DEF_TEST
#define DEF_TEST
#include <iostream>
#include <vector>
class Test
{
public:
Test();
private:
std::vector<double> a(0);
};
#endif
,這是TEST.CPP:
#include "test.hpp"
Test::Test()
{
a.push_back(2.3);
std::cout << a[0] << std::endl;
}
,編譯器告訴我:
In file included from main.cpp:1:0:
test.hpp:11:23: error: expected identifier before numeric constant
std::vector<double> a(0);
^
test.hpp:11:23: error: expected ‘,’ or ‘...’ before numeric constant
In file included from test.cpp:1:0:
test.hpp:11:23: error: expected identifier before numeric constant
std::vector<double> a(0);
^
test.hpp:11:23: error: expected ‘,’ or ‘...’ before numeric constant
test.cpp: In constructor ‘Test::Test()’:
test.cpp:5:1: error: ‘((Test*)this)->Test::a’ does not have class type
a.push_back(2.3);
^
test.cpp:6:17: error: invalid types ‘<unresolved overloaded function type>[int]’ for array subscript
std::cout << a[0] << std::endl;
謝謝你的幫助!