c++ - 如何在 C++ 中定义 vector ?

标签 c++ vector makefile

我已经复制粘贴了我的代码和我得到的错误。这很奇怪。我已经标记了我遇到错误的所有行。我什至无法编译类。

#ifndef MYRECORD_H
#define MYRECORD_H
#include "MyException.h"
#include <map>

using namespace std;
//Error in next line
using std::vector;

class MyRecord {
public:
    explicit MyRecord(string& id); // I don't want implicit conversion from std::string to MyRecord!
    // Mutator functions - setting of other fields
    // In this version, hardly any of the implementations will actually throw exceptions
    // later validation functions will be added
    void setName(string& aname) throw (MyException);
    void setEmail(string& amail) throw (MyException);
    void setInfo(string& info) throw (MyException);
    void setImage(string& imagestr) throw (MyException);
    void addRole(string& anotherrole) ;
    void addKeyValue(string& collectionname, string& key, string& value) throw(MyException);
    // Accessor functions

    string getID() const { return this->id; }
    string getName() const { return this->name; }
    string getEmail() const { return this->email; }
    string getInfo() const { return this->info; }
    string getImage() const { return this->image; }
    string getAttribute(string& collectionname, string& key) const throw(MyException);
//Error in next line
    const vector<string>& getRoles() const { return this->roles; }
//Error in next line
    const map<string,string>& getPhones() const { return this->phones; }
    const map<string,string>& getAddresses() const { return this->addresses; }
    const map<string,string>& getOtherKV() const { return this->other; }

    bool hasRole(string& queryrole) const;

private:
    string id; // Also known as "nickname", or even "primary key"
    string name; // full name
    string email; 
    string image;
    string info;
//Error in next line
    vector<string> roles;
    map<string,string> phones;
    map<string,string> addresses;
    map<string,string> other;
     // Finally, note that copy constructor and assignment operator are
    // private (and no implementations will be defined).  I choose to 
    // disallow such operations.
    MyRecord(const MyRecord& orig);
    MyRecord& operator=(const MyRecord&);
    // Further there is no virtual destructor, I do not intend MyRecord
    // to be the base class in some hierarchy.
};

#endif  /* MYRECORD_H */

错误拷贝

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/myrecordproject
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/MyRecord.o.d
g++    -c -g -I/usr/local/include/cppunit -I/usr/local/include/boost -MMD -MP -MF build/Debug/GNU-MacOSX/MyRecord.o.d -o build/Debug/GNU-MacOSX/MyRecord.o MyRecord.cpp
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++    -c -g -I/usr/local/include/cppunit -I/usr/local/include/boost -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
In file included from main.cpp:12:
MyException.h:14: error: expected unqualified-id before 'using'
In file included from main.cpp:13:
MyRecord.h:15: error: 'std::vector' has not been declared
MyRecord.h:37: error: ISO C++ forbids declaration of 'vector' with no type
MyRecord.h:37: error: expected ';' before '<' token
MyRecord.h:38: error: expected `;' before 'const'
MyRecord.h:50: error: ISO C++ forbids declaration of 'vector' with no type
MyRecord.h:50: error: expected ';' before '<' token
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

最佳答案

您需要在定义 std::vector 模板的位置包含 vector header

#include <vector>

关于c++ - 如何在 C++ 中定义 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22648361/

相关文章:

c++ - 如何保证两个线程中的两个类写同一个变量线程安全?

c++ - 有什么方法可以防止命名空间在 C++ 中添加更多类?

c++ - set 和 vector 哪个效率更高

c++ - 如何对具有两个变量的 vector 进行排序?

c++ - 在 Mac 上安装 xgboost 失败 - ar : no archive members specified

c++ - math.h 即使包含在 makefile 中也不被包含

c++ - 在 C++ 中以双指针作为默认参数递归调用函数时出现运行时错误

c++ - 在 C++ 中抛出和捕获自定义异常的问题

c++ - 为什么这个 vector 声明无效?

foreach - GNU Make 失败,foreach->eval->call