Card and Deck: Problem linking classes (environment issues?)
EDIT: I removed the main from Card.cpp, and run
C:\cpp>g++ Deck.cpp Card.cpp
C:\cpp>g++ Deck.cpp Card.cpp
In file included from Deck.h:8:0,
from Deck.cpp:1:
Card.h: In member function 'Card& Card::operator=(const Card&)':
Card.h:13:12: instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(
std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = Card, _Alloc = std::
allocator<Card>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterat
or<Card*, std::vector<Card> >, typename std::vector<_Tp, _Alloc>::_Base:开发者_如何学Go:_Tp_all
oc_type::pointer = Card*]'
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:749:4: ins
tantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [wit
h _Tp = Card, _Alloc = std::allocator<Card>, value_type = Card]'
Deck.cpp:7:44: instantiated from here
Card.h:13:12: error: non-static const member 'const int Card::rank', can't use d
efault assignment operator
Card.h:13:12: error: non-static const member 'const Suit Card::suit', can't use
default assignment operator
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/vector:6
9:0,
from Deck.h:4,
from Deck.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc: In member fun
ction 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::it
erator, const _Tp&) [with _Tp = Card, _Alloc = std::allocator<Card>, std::vector
<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Card*, std::vector<Card>
>, typename std::vector<_Tp, _Alloc>::_Base::_Tp_alloc_type::pointer = Card*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc:312:4: note: s
ynthesized method 'Card& Card::operator=(const Card&)' first required here
I don't know what the problem is. Someone else tells me that it compiled perfectly fine on their computer. Anybody know what's going on?
Card.h
#ifndef CARD_H
#define CARD_H
#include <string>
enum Suit {
SUIT_HEART,
SUIT_DIAMOND,
SUIT_CLUB,
SUIT_SPADE
};
class Card {
private:
const int rank;
const Suit suit;
static const char * ranknames[];
static const char * suitnames[];
public:
Card(int r = 1, Suit s = SUIT_HEART) : rank(r), suit(s)
{
}
int GetRank() const { return rank; };
Suit GetSuit() const { return suit; }
std::string ToString() const;
std::string SuitString() const;
std::string RankString() const;
};
#endif
Card.cpp
#include <iostream>
#include "Card.h"
#include <vector>
const char * Card::ranknames[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
const char * Card::suitnames[] = { "Hearts", "Diamonds", "Clubs", "Spaces" };
std::string Card::ToString() const {
std::string s = RankString();
s.append(" of ");
s.append(SuitString());
return s;
}
std::string Card::SuitString() const {
return suitnames[suit];
}
std::string Card::RankString() const {
return ranknames[rank-1];
}
Deck.h
#ifndef DECK_H
#define DECK_H
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include "Card.h"
#endif
Deck.cpp
#include "Deck.h"
int main() {
std::vector<Card> Deck;
for (int i = 0; i < 10 ; i++) {
Deck.push_back(Card(i+1,(Suit)((i+1)%4)));
std::cout << Deck[i].ToString() << std::endl;
}
}
Did you try specifying both modules on the command line?
g++ -o deck.exe deck.cpp card.cpp
Edit: Also, using constants that start with _ or __ is technically illegal. For include-guards, I prefer to use the form MODULE_H
You're supposed to compile and link Card.cpp
and Deck.cpp
together in a single executable, which I presume you don't as both files contain a main
function :
g++ Deck.cpp Card.cpp
Of course, you'll have to remove one of the two main
if you don't want to get into a new linker error.
精彩评论