Question

I have a class "board" that I am trying to put a deque member in. I wrote the code with an int object for the deque and everything worked fine, so I think it is a problem with setting the Template for the custom class, but I have never done this in C++.

board.h:

 #ifndef __board_h__
 #define __board_h__
 using namespace std;

 #include <deque>
 #include "noble_card.h"
 class board;

 class board
{
    public: deque<noble_card> line;
    public: board();
    public: ~board();
};

#endif

board.cpp:

#include <deque>
#include "noble_card.h"

board::board() {
    deque<noble_card> line;
}

board::~board() {}

I think I may have a problem with the construction method here, as deque is erroring out on so many things I am having trouble tracking it down.

noble_card.h:

#include <string>

#ifndef __noble_card_h__
#define __noble_card_h__

#include "board.h"

class noble_card
{
    public: string name;
    public: int id;
    public: int vp;

    public: noble_card(int _vp);
    public: ~noble_card();
};

#endif

noble_card.cpp:

#include "noble_card.h"

noble_card::noble_card(int _vp) {
        this->vp = _vp;
}

noble_card::~noble_card() {
}

Now, the problem comes when I try to push elements onto this deque, i have a for loop like such:

board b;
for (unsigned i = 0; i < 12; i++) {
          noble_card nc(i);
          b->line.push_back( nc );
}

I keep getting assignment operator could not be generated, copy constructor could not be generated, and std::deque : 'noble_card' is not a valid template type argument for parameter '_Ty' (board.h). I am assuming this is because I haven't templated my classes and overridden the copy/constructor methods to tell the deque how to sort/remove/alloc/copy this type of class. I'm basically just trying to get a custom c++ class to be used in a deque and it is a lot more complicated than C# and other standard libraries I've used where you just push it on there and it takes care of it.

EDIT:

#ifndef __noble_card_h__
#define __noble_card_h__
using namespace std;

class noble_card {
    public: char* name;
    public: int id;
    public: int vp;

    public: noble_card(char* _name, int _id, int _vp) : name(_name), id(_id), vp(_vp) {}
};

#endif

Setting up noble_card.h this way seems to satisfy the requirements for the copy/alloc/constructor for deque. I still don't fully understand it as it seems to be shorthand, so if anyone could expand on line 10 I'd much appreciate it. For now This change has me moving forward.

Was it helpful?

Solution

Small fix of your immediate problem:

Your variable b is not a pointer and the operator->

b->line.push_back(nc); 

will therefore not work. You have to use the operator.

b.line.push_back(nc);

C++ is not C# (mini code review)

You write that you are coming from C#. There are a lot of stylistic differences between the two languages that you should be aware off. I don't know C#, but here is a 2nd take on your code, taking care of the sorest points (I'm commenting out the header inclusions as it doesn't work on the online compiler I use)

// noble_card.h
#include <string>

class noble_card
{
public: // single section of public stuff (are you sure you don't need private data?)
    std::string name; // never do: "using namespace std;" in a header!
    int id;
    int vp;

    noble_card(int _vp);
    // compiler-generated default constructor is just fine here
};

// noble_card.cpp

noble_card::noble_card(int _vp): vp(_vp) {} // initialize member in initializer-list

// board.h
// #include "noble_card.h"
#include <deque>

class board
{
public: // single section of public stuff (are you sure you don't need private data?)
    std::deque<noble_card> line; // never do: "using namespace std;" in a header!
    // compiler generated default constructor and destructor are just fine here
};

// board.cpp (not necessary for your current impl)
// #include "board.h" // takes care of "noble_card.h"

// main.cpp

int main()
{
    board b;
    for (unsigned i = 0; i < 12; ++i) { 
        b.line.emplace_back(i); // C++11 allows you to construct-in-place
    }
}

Live Example.

You should Google this site to get in-depth explanations of the points that I write in the above code behind the // comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top