views:

76

answers:

1

hey I i am writing a Program in C++ and for some reason the compiler finds errors , and cant find my constructor: here is my class: (inside a h file)

class Adjutancy
{
private:
 vector<Vehicale*,CompareCatId>* m_vehicalesVector;
 map<const string,Base*>* m_baseMap;
 map<const int,City*>* m_citiesMap;
 vector<vector<Distance*>>* m_distancesMatrix;
public:

 Adjutancy(vector<Vehicale*,CompareCatId>* vehicalesVector , map<const string,Base*>* baseMap , map<const int,City*>* citiesMap , vector<vector<Distance*>>* distancesMatrix);
};

and here is my constructor implementation inside a cpp file: sorry about the length of it.

Adjutancy::Adjutancy(vector<Vehicale*,
  CompareCatId>* vehicalesVector,map<const string,Base*>* baseMap,
  map<const int,City*>* citiesMap,
  vector<vector<Distance*>>* distancesMatrix):
      m_vehicalesVector(vehicalesVector),
      m_baseMap(baseMap),
      m_citiesMap(citiesMap),
      m_distancesMatrix(distancesMatrix)
{}

for some reason inside my main i cant use the constructor. thanks in advance.

+2  A: 

I suppose you mean this setup:

// file adjutancy.h
// ... includes for all classes used below
// forward declarations are OK for pointers and references
class Adjutancy
{
public:
    Adjutancy(vector<Vehicale*,CompareCatId>* vehicalesVector , map<const string,Base*>* baseMap , map<const int,City*>* citiesMap , vector<vector<Distance*>>* distancesMatrix);

private:
    vector<Vehicale*,CompareCatId>* m_vehicalesVector;
    map<const string,Base*>* m_baseMap;
    map<const int,City*>* m_citiesMap;
    vector<vector<Distance*>>* m_distancesMatrix;
};

// file adjutancy.cpp
#include "adjutancy.h"
Adjutancy::Adjutancy(vector<Vehicale*,CompareCatId>* vehicalesVector,map<const string,Base*>* baseMap,map<const int,City*>* citiesMap,vector<vector<Distance*>>* distancesMatrix)
:   m_vehicalesVector(vehicalesVector),
    m_baseMap(baseMap),
    m_citiesMap(citiesMap),
    m_distancesMatrix(distancesMatrix)
{   }

This is horrible code, and for several reasons:

  1. You're storing a pointer to std::vectors and std::maps in your class: just store the vectors and maps themselves, no pointers needed here. Alternatively, store a reference, but beware of the limitations (no un-initialized reference data members!).
  2. You should avoid template type lists with >>, put a space between the two, or some compilers will misinterpret it as operator>>.
  3. Do not use using statements in your header, instead, use the full namespace prefix.

I can't say what's wrong (everything looks correct at first glance) until you show me your main (complete!) function and how you want to use your constructor.


Better version:

// file adjutancy.h

#include <map>
#include <string>
#include <vector>
class Adjutancy
{
public:
    Adjutancy( std::vector<Vehicale*,CompareCatId> &vehicalesVector,
               std::map<const std::string,Base*> &baseMap,
               std::map<const int,City*> &citiesMap,
               std::vector<std::vector<Distance*> > &distancesMatrix );

private:
    std::vector<Vehicale*,CompareCatId> &m_vehicalesVector;
    std::map<const std::string,Base*> &m_baseMap;
    std::map<const int,City*> &m_citiesMap;
    std::vector<std::vector<Distance*> > &m_distancesMatrix;
};

// file adjutancy.cpp
#include "adjutancy.h"

#include <map>
    using std::map;
#include <string>
    using std::string;
#include <vector>
    using std::vector;

Adjutancy::Adjutancy( vector<Vehicale*, CompareCatId> &vehicalesVector,
                      map<const string, Base*> &baseMap,
                      map<const int, City*> &citiesMap,
                      vector<vector<Distance*> > &distancesMatrix )
:   m_vehicalesVector( vehicalesVector ),
    m_baseMap( baseMap ),
    m_citiesMap( citiesMap ),
    m_distancesMatrix( distancesMatrix )
{   }

Also remember: "vehicle" is the correct English spelling.

rubenvb