C++ C++;向量类作为其他类中的成员
请告诉我这个代码,它给了我很多错误:C++ C++;向量类作为其他类中的成员,c++,vector,C++,Vector,请告诉我这个代码,它给了我很多错误: //Neuron.h File #ifndef Neuron_h #define Neuron_h #include "vector" class Neuron { private: vector<double>lstWeights; public: vector<double> GetWeight(); }; #endif //Neuron.cpp File #include "Neuron.h" vector<dou
//Neuron.h File
#ifndef Neuron_h
#define Neuron_h
#include "vector"
class Neuron
{
private:
vector<double>lstWeights;
public:
vector<double> GetWeight();
};
#endif
//Neuron.cpp File
#include "Neuron.h"
vector<double> Neuron::GetWeight()
{
return lstWeights;
}
//Neuron.h文件
#ifndef神经元
#定义神经元
#包括“向量”
类神经元
{
私人:
矢量权重;
公众:
向量GetWeight();
};
#恩迪夫
//Neuron.cpp文件
#包括“Neuron.h”
向量神经元::GetWeight()
{
返回权重;
}
谁能告诉我它有什么问题吗?它是:
#include <vector>
您需要在正确的命名空间中限定向量:
class Neuron
{
private:
std::vector<double>lstWeights;
public:
std::vector<double> GetWeight();
};
std::vector<double> Neuron::GetWeight()
\ifndef Neuron\u h
#定义神经元
#包括“向量”
使用std::vector;
类神经元
{
私人:
矢量权重;
公众:
向量GetWeight();
};
#恩迪夫
尝试用
std::vector
替换vector
,a la:
std::vector<double> lstWeights;
std::向量权重;
问题是标准容器位于标准名称空间中,因此您必须以某种方式让编译器知道您希望使用标准名称空间的vector版本;你可以用几种方法中的一种来完成这项工作,std::vector
是最明确的。用std:
作为vector
的前缀,例如std::vector
,应该完成这项工作。首先,包括。注意角括号
其次,它是“std::vector”,而不仅仅是“vector”(或使用“using”指令)
第三,不要按值返回向量。这是沉重的,通常完全没有必要。而是返回[const]引用
class Neuron {
private:
std::vector<double> lstWeights;
public:
const vector<double>& GetWeight() const;
};
const std::vector<double>& Neuron::GetWeight() const
{
return lstWeights;
}
类神经元{
私人:
向量权重;
公众:
常量向量&GetWeight()常量;
};
常量std::vector&Neuron::GetWeight()常量
{
返回权重;
}
通常不会没有错误消息……发布更多细节、错误消息、更多代码等等。你是说std::vector?你想每次调用GetWeight()时都返回整个verctor的副本吗?为了社区的利益,如果你接受了最好的答案(对于这个问题和你的其他问题),这会很有帮助。@ChrisW总的来说是个坏主意,在另一个库中可能有一个vector类。@ChrisW我相信你知道,但只是为了作者的缘故,您不应该在标题中包含所有的std::
。只需在#include
语句之后使用std::vector
,或者将其引用为std::vector
time@GMan,使用“vector”而不是将只会降低预处理器的速度。使用引号而不是尖括号只是告诉编译器首先查看当前目录,如果没有找到任何内容,请查看相应的include dirsRight,这就是为什么应该这样做。也许我会说得更清楚一点,除非OP在同一个目录中有一个名为“vector”的库,否则这仍然行不通。他的意思很可能是“包括”
const container_type& GetWeight() const; // const because GetWeight does
// not modify the class
#ifndef Neuron_h
#define Neuron_h
#include "vector"
using std::vector;
class Neuron
{
private:
vector<double>lstWeights;
public:
vector<double> GetWeight();
};
#endif
std::vector<double> lstWeights;
class Neuron {
private:
std::vector<double> lstWeights;
public:
const vector<double>& GetWeight() const;
};
const std::vector<double>& Neuron::GetWeight() const
{
return lstWeights;
}