Piero
Piero

Reputation: 9273

conversion structured list c++ passing a list from a class to another

i have a problem with a list structured that i can't resolve, i want pass this list from a class to another class, this is my code:

ClassA.h

#include <list>

class ClassA
{
public:
   struct MyStruct
   {
    int a;
    int b;
   };

   std::list<MyStruct> myList;
   void fillList();
}

ClassA.ccp

#include "ClassA.h"
#include <fstream>
#include <iostream>

using namespace std;

void ClassA::fillList () {
  MyStruct c;
  c.a = 1;
  c.b = 2;
  myList.push_back(c);
}

ClassB.h

#include <list>

class ClassB
{
   struct MyStruct
   {
    int a;
    int b;
   };

   std::list<MyStruct> myListB;
   public:
      ClassB(std::list<MyStruct> l);
}

ClassB.ccp

#include "ClassB.h"
#include <fstream>
#include <iostream>

using namespace std;

ClassB::ClassB(std::list<MyStruct> l)
{
   myListB = l;
}

main.ccp

#include <iostream>
#include "ClassA.h"
#include "ClassB.h"

using namespace std;

int main()
{
ClassA a;
a.fillList();

ClassB(a.myList); //I receive the error here
}

I receive the error where i comment here above, and the error is no matching conversion for functional-style cast from'std::list<MyStruct>' to ClassB, how i wrong? how i can pass the list between the two class? please help!

EDIT: i do as a.lasram suggest in the answer so now i have this:

#include <list>
#include "ClassA.h" //i add this beacuse ask me and now i have a new error
class ClassB
{
typedef ClassA::MyStruct MyStruct;

std::list<MyStruct> myListB;
public:
  ClassB(std::list<MyStruct> l);
};

after this edit, the old error is resolved, but there is new one here:

ClassA.h

#include <list>

class ClassA //<-- here the error is: Redefinition of ClassA
{....
 ....
}

how i can solve? please help...

EDIT 2: i solve the second error with this:

#ifndef __Terzo_Progetto__ClassA__
#define __Terzo_Progetto__ClassA__

#include <list>

class ClassA
{...
}

#endif

Upvotes: 2

Views: 63

Answers (1)

a.lasram
a.lasram

Reputation: 4411

ClassA::MyStruct and ClassB::MyStruct are different. You can solve this by declaring ClassB as follows:

class ClassB
{
   typedef ClassA::MyStruct MyStruct;

   std::list<MyStruct> myListB;
   public:
      ClassB(std::list<MyStruct> l);
};

Upvotes: 2

Related Questions