卡和甲板:链接类的问题(环境问题?)

时间:2010-11-25 17:55:08

标签: c++

编辑:我从Card.cpp中删除了主页,然后运行

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::_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

我不知道问题是什么。别人告诉我,它在他们的计算机上编译完全正常。有谁知道发生了什么事?

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

}

2 个答案:

答案 0 :(得分:1)

您是否尝试在命令行中指定两个模块?

g++ -o deck.exe deck.cpp card.cpp

编辑:此外,使用以_或__开头的常量在技术上是非法的。对于包含警员,我更喜欢使用MODULE_H

形式

答案 1 :(得分:1)

你应该在一个可执行文件中编译和链接Card.cpp Deck.cpp,我认为你不会这两个文件包含{{1功能:

main

当然,如果您不想进入新的链接器错误,则必须删除两个g++ Deck.cpp Card.cpp 中的一个。