samedi 22 avril 2017

How to declare a vector in a hpp?

Hello I know that to declare a std::vector I have to do like this

std::vector<double> a(0);

But in my file it does not work. Here is my code :

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

and this is test.cpp :

#include "test.hpp"

Test::Test(){

a.push_back(2.3);
std::cout << a[0] << std::endl;

}

And the compiler told me :

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;

Thank you for your help !

Aucun commentaire:

Enregistrer un commentaire