我有这个代码,它给了我很多错误:
//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;
}有人能告诉我它出了什么问题吗?
发布于 2009-10-13 21:52:46
它是:
#include <vector>你使用尖括号是因为它是standard library的一部分,"“只会让编译器首先在其他目录中查找,这是不必要的缓慢。并且它位于命名空间std中
std::vector<double>您需要在正确的名称空间中限定您的向量:
class Neuron
{
private:
std::vector<double>lstWeights;
public:
std::vector<double> GetWeight();
};
std::vector<double> Neuron::GetWeight()使用typedef的使其更简单:
class Neuron
{
public:
typedef std::vector<double> container_type;
const container_type& GetWeight(); // return by reference to avoid
// unnecessary copying
private: // most agree private should be at bottom
container_type lstWeights;
};
const Neuron::container_type& Neuron::GetWeight()
{
return lstWeights;
}还有,别忘了做const-correct
const container_type& GetWeight() const; // const because GetWeight does
// not modify the class发布于 2009-10-13 21:56:48
首先是#include <vector>。请注意尖括号。
其次,它是'std::vector',而不仅仅是'vector‘(或者使用'using’指令)。
第三,不要通过值返回向量。这是很繁重的,通常是完全不必要的。改为返回常量引用
class Neuron {
private:
std::vector<double> lstWeights;
public:
const vector<double>& GetWeight() const;
};
const std::vector<double>& Neuron::GetWeight() const
{
return lstWeights;
}发布于 2009-10-13 21:53:17
#ifndef Neuron_h
#define Neuron_h
#include "vector"
using std::vector;
class Neuron
{
private:
vector<double>lstWeights;
public:
vector<double> GetWeight();
};
#endif试试看
https://stackoverflow.com/questions/1563124
复制相似问题