tags:

views:

713

answers:

5

Please I have this code which gives me many errors:

//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;
}

Could any one tell me what is wrong with it?

+12  A: 

It's:

#include <vector>

You use angle-brackets because it's part of the standard library, "" with just make the compiler look in other directories first, which is unnecessarily slow. And it is located in the namespace std:

std::vector<double>

You need to qualify your vector in the correct namespace:

class Neuron
{
private:
 std::vector<double>lstWeights;
public:
 std::vector<double> GetWeight();

};

std::vector<double> Neuron::GetWeight()

Made simpler with typedef's:

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;
}

Also, don't forget to be const-correct:

const container_type& GetWeight() const; // const because GetWeight does
                                         // not modify the class
GMan
or `using namespace std;`
ChrisW
@ChrisW bad idea in general, there could be a vector class in another library.
klez
@ChrisW I'm sure you know, but just for the author's sake, you shouldn't include all of `std::` in a header. Either just do `using std::vector` after the `#include` statement, or reference it as `std::vector<T>` every time
Andrew Song
@GMan, using "vector" instead of <vector> will just slow down the preprocessor. Using quotes instead of angular brackets just tells the compiler to look first in the current directory, and if nothing found, look in the appropriate include dirs
klez
Right, that's why it should be <>. Maybe I'll make it more clear.
GMan
A: 
#ifndef Neuron_h
#define Neuron_h
#include "vector"

using std::vector;

class Neuron
{
private:
 vector<double>lstWeights;
public:
 vector<double> GetWeight();

};
#endif

try that

Mark P Neyer
That still won't work, unless OP has a library named "vector" in the same directory. He likely meant `#include <vector>`
Andrew Song
s/won't work/isn't really the most correct
Andrew Song
good point, sir
Mark P Neyer
A: 

try replacing vector with std::vector, a la:

std::vector<double> lstWeights;

The issue is that the standard containers are in the standard namespace, so you have to somehow let the compiler know that you'd like to use the standard namespace's version of vector; you do that one of several ways, std::vector being the most explicit.

fbrereto
A: 

Prefixing vector<double> with std::, e.g. std::vector<double>, should do the work.

klez
+1  A: 

Firstly, #include <vector>. Note the angular brackets.

Secondly, it's 'std::vector', not just 'vector' (or use 'using' directive).

Thirdly, don't return vectors by value. This is heavy and usually completely unnecessary. Return a [const] reference instead

class Neuron {
private: 
    std::vector<double> lstWeights;
public: 
    const vector<double>& GetWeight() const;
};    

const std::vector<double>& Neuron::GetWeight() const
{ 
  return lstWeights;
}
AndreyT