在 C++ 类定义中使用向量

using vector in c++ class definition

本文关键字:向量 定义 C++      更新时间:2023-10-16

我有一个神秘的问题。在尝试在旅游类中定义变量时,我一直得到一个"vector"没有命名类型错误。该库似乎安装正确,因为我可以在 main 中定义向量。根据这里的许多帖子,我已经检查了正确的向量包含和 std 命名空间。我仍然收到错误。

主.cpp

#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;
int main () {
        //vector declaration works here
return 0;
}

图尔·

#ifndef TOUR_H_
#define TOUR_H_
#include<vector>
using namespace std;
class tour
{
    public:
      //variables
      int teamID;
      vector<int> tourseq; //this is where the error occurs
      //functions
      tour(int);
};
#endif /* TOUR_H_ */

旅游.cpp

#include<vector>
#include "tour.h"
using namespace std;
tour::tour(int id){
teamID = id;
}

这里可能出了什么问题?

与其写using namespace std;vector<int> tourseq;不如考虑写std::vector<int> tourseq;

无论如何,您可能不应该在代码中放入using namespace std;