error LNK2019: unresolved external symbol

前端 未结 2 1038
灰色年华
灰色年华 2020-12-09 12:04

I\'ve recently started to program in C++ again, and for the purposes of education, I am working on creating a poker game. The weird part is, I keep getting the following err

相关标签:
2条回答
  • 2020-12-09 12:19

    Another solution could be: check the cmake file and make sure it (such as in ADD_EXECUTABLE) includes the .cpp file you listed.

    0 讨论(0)
  • 2020-12-09 12:31

    Because of the comment below, I've rewritten what I had before.

    The problem that the linker is complaining about is that you've declared your member functions in Poker, but haven't defined them. How is this? For starters, you're creating a new class and defining separate member functions in it.

    Your header file Poker class exists in the PokerGame namespace and your cpp file Poker class exists in the global namespace. To fix that issue, put them in the same namespace:

    //cpp file
    namespace PokerGame {
        class Poker {
            ...
        };
    }
    

    Now that they're in the same namespace, you have another issue. You're defining your member functions inside the class body, but not the first one. The definitions simply can't go in the body of a class named the same way. Get rid of the whole class in the cpp file:

    //cpp file
    namespace PokerGame {
        Poker::Poker() {
            deck = Deck(); //consider a member initializer instead
        }
    
        //other definitions
    }
    

    One last thing: you put the private section of your class in the wrong spot. It was in that cpp file class that we just removed. It belongs with the other parts of your class:

    //header file
    namespace PokerGame {
        class Poker {
        public:
            //public stuff
    
        private: 
            Deck deck; //moved from cpp file
       };
    }
    
    0 讨论(0)
提交回复
热议问题