开发者

PersonalVec.hpp:12: error: expected unqualified-id before ‘;’ token error

开发者 https://www.devze.com 2023-04-06 04:23 出处:网络
I got this error and I can\'t figure out why. #inclu开发者_如何学Pythonde <vector> #include <cstdlib>

I got this error and I can't figure out why.

#inclu开发者_如何学Pythonde <vector>
#include <cstdlib>

#ifndef PERSONALVEC_HPP_
#define PERSONALVEC_HPP_

template <class T,class PrnT>

class PersonalVec
{
public:
    PersonalVec() {}

    ~PersonalVec()
    {
        //TODO: delete vector.
    }

    void push_back(T& obj)
    {
        int index = rand()%_vec.size();
    }

private:
    vector<T*> _vec;
};

#endif /* PERSONALVEC_HPP_ */


Both rand and vector are in the std namespace. Use

private:
std::vector<T*> _vec;

and

std::rand() 


On this line:

int index = rand()%_vec.size(); 

You call the function rand() but do not include the header which declares it. Specifically, you need to add the following line to the top of your program:

#include <cstdlib>


Part of the problem is likely that you are using a vector without being in the std namespace. change vector<T*> _vec to std::vector<T*> _vec.

The following code (Ideone linky: http://www.ideone.com/HgL1e) seems to work fine.

#include <vector> 
#include <cstdlib> 

template <class T,class PrnT> 
class PersonalVec 
{ 
public: 
    PersonalVec() {} 

    ~PersonalVec() 
    { 
        //TODO: delete vector. 
    } 

    void push_back(T& obj) 
    { 
        int index = rand()%_vec.size(); 
    } 

private: 
    std::vector<T*> _vec; 
}; 

int main()
{
    int i = 1;
    PersonalVec<int, int> testVec;
    testVec.push_back(i);
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消