0

So I have been getting an issue where when I am compiling my classes along with the main .cpp I am getting a linking error. I am not the most versed with C++ as this is my first year coding in this language. I am using VS Code for this as well.

My Team Class:

#ifndef TEAM_H
#define TEAM_H
#include "Player.h"
#include <vector>
using namespace std;

class Team{
    public:
        Team();
        void setGold(int _Gold);
        int getGold();
        void changeGold(int _Gold);
        void setIngredients(int _Ingredients);
        int getIngredients();
        void changeIngredients(int _Gold);
        void setCookware(int _Cookware[3]);
        void changeCookware(int _Cookware[3]);
        void setArmor(int _Armor);
        int getArmor();
        void changeArmor(int _Armor);
        void addWeapon(string _Name, string _Level);
        string removeOneWeapon();
        int getWeapons();
        int getPlayers();
        void setKey(bool _Key);
        bool getKey();
        void updateFull(int _Fullness);
        static string weapons[5][2]; //First index is name, Second is level
                            //"NULL" and "NULL" = no weapon,
        static int cookware[3];//0,1,2 index's equate to number of that type of cookware
        vector<string> getTreasures();
        int getTreasureSize();
        static Player players[5];
    private:
        int gold;
        int ingredients;
        int armor;
        bool key;
        
        vector<string> treasures;
         
};
#endif

A function in another class that uses the weapons and Player array

void Game::startGame(){
    string leaderName;
    string playerOne, playerTwo, playerThree, playerFour;
    monster1.readMonsterFile("monsters.txt");
    cout << "Enter name of party leader:" << endl;
    cin >> leaderName;
    Team::players[0].setName(leaderName);
    cout << "Enter the names of other party members:" << endl;
    cin >> playerOne >> playerTwo >> playerThree >> playerFour;
    Team::players[1].setName(playerOne);
    Team::players[2].setName(playerTwo);
    Team::players[3].setName(playerThree);
    Team::players[4].setName(playerFour);
    cout << "Welcome party leader " << leaderName << " and his/her companions " << playerOne << playerTwo << playerThree << playerFour << endl;
    
}

And then the error i am getting:

Williams-MacBook-Pro:Project3 William$ g++ -std=c++11 Player.cpp Team.cpp Monster.cpp Game.cpp main.cpp
Undefined symbols for architecture x86_64:
  "Team::players", referenced from:
      Team::getPlayers() in Team-b8a69d.o
      Team::updateFull(int) in Team-b8a69d.o
      Game::startGame() in Game-98f56e.o
      Game::statusUpdate() in Game-98f56e.o
  "Team::weapons", referenced from:
      Team::Team() in Team-b8a69d.o
      Team::getWeapons() in Team-b8a69d.o
      Team::addWeapon(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in Team-b8a69d.o
      Team::removeOneWeapon() in Team-b8a69d.o
      Monster::attack(int, int, int) in Monster-7397ef.o
      Game::statusUpdate() in Game-98f56e.o
  "Team::cookware", referenced from:
      Team::Team() in Team-b8a69d.o
      Team::setCookware(int*) in Team-b8a69d.o
      Team::changeCookware(int*) in Team-b8a69d.o
      Game::statusUpdate() in Game-98f56e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any help is much appreciated!

  • Welcome to StackOverflow. I believe your question is already answered in the given duplicate. Long story short: you need to define `static` member variables in an implementation file, not in a header. For a rationale, see the linked question. If you believe that this question cannot get answered by the given duplicate, feel free to comment on this post and @me. – Zeta Apr 22 '21 at 21:01
  • Note: getters and setters for everything is usually a bad idea. Prefer to have functions that ask the object to modify itself to other objects getting information, modifying that information and then storing the result back in the object. The less outsiders know about what's really going on in an object, and the fewer decisions the outsider makes for an object, the better. This is Encapsulation. Making a member `private` is tsa step in the right direction, but letting anyone get and set it through `public` functions, effectively undoes `private` and breaks encapsulation. – user4581301 Apr 22 '21 at 21:05
  • A `static` list of members in a `Team` means you can only ever have one `Team` as all `Team`s will have the same list. If you will only ever have one time, then making the list `static` is moot. I recommend rethinking this choice. – user4581301 Apr 22 '21 at 21:08

0 Answers0