Member Avatar for Nathan_6

Hi everyone.

I am makng a RISK game and I am working on a territory class here, and I got a strange message that my google searches have not shed any light on.

My code is below.

First, my header file:

//  Class represents each individual territory on the map

#ifndef Territory_hpp
#define Territory_hpp

#include <string>
#include <vector>
#include <array>
#include <map>
using std::string;

class Unit; //forward declaration of class unit
////////////////////////////////////////////////////////


class Territory
{
public:


    //CONTINENTS represents the index of the contninents in the corresponding vector ALL_BOARD_TERRITORIES
    //client classes can also use CONTINENTS to initialize a territory
    enum CONTNINENTS : int {NORTH_AMERICA = 0, SOUTH_AMERICA = 1,
        AFRICA = 2, AUSTRALIA = 3, ASIA=4, EUROPE=5};

    static const std::array<std::vector<string>,6> ALL_BOARD_TERRITORIES;

    ///////////////////////////////////////////////////////////////
    Territory(const std::string&,const std::string&,const std::string&);
    ~Territory();
    //RETURN TO QUESTION OF DESTRUCTOR//

    std::string getTerritoryName() const;
    std::string getContinent() const;

    std::string getCurrentOwner() const;
    void setCurrentOwner(const std::string&);

    int getNumUnitsInTerritory() const; //returns the number of "single units" (i.e. infantry)

    bool borders(const Territory&) const;

    //CHECK IF CONST QUALIFIER IS NEEDED IN HERE
    Unit* removeUnit(Unit*);     //removes a unit from this territory
    void takeInUnit(Unit*);     //adds a unit to this territory

    friend std::ostream& operator<<(std::ostream&,const Territory&);

private:
    std::string territoryName;
    std::string continent;
    std::string currentOwner;
    std::vector<Unit*> unitsInTerritory;//represents all the units in a territory
    int numSingleUnitCount; //represents the current count of the number of single infantry units in the territory [that is a cannon == 5 infantry] and sof orth


    CHECK OUT ADDITIONAL COMMENTS TO FIX

    //represents what countries border what
    static const std::map<std::string,std::vector<std::string>> COUNTRIES_BORDERING;
};

//represents all the territories on the map
std::array<std::vector<string>,6> Territory::ALL_BOARD_TERRITORIES
{
    {   //NORTH AMERICA
        "Alaska","Northwest Territory","Greenland","Alberta","Ontario","Eastern Canada","Western United States","Eastern United States","Central America"

    },
    {   //SOUTH AMERICA
        "Venezuela","Brazil","Peru","Argentina"

    },
    {   //AFRICA
        "North Africa","Egypt","East Africa","Central Africa","South Africa","Madagascar"
    },
    {   //AUSTRALIA
        "Indonesia","New Guinea","Western Australia","Eastern Australia"
    },
    {   //ASIA
        "Middle East","Afghanistan","India","Southeast Asia","China","Mongolia","Japan","Kamchatka","Irkutsk","Yakutsk","Siberia","Ural"
    },
    {   //EUROPE
        "Russia","Scandinavia","Iceland","Great Britain","Western Europe","Northern Europe","Southern Europe"
    }
};

//represents each territory and what other territories border it
std::map<std::string,std::vector<std::string>> Territory::COUNTRIES_BORDERING
{
    //format of the values in the map is: <<COUNTRY>,{Countries that border}>


    //North America
    { "Alaska", {"Kamchatka","Northwest Territory","Alberta"}},
    { "Northwest Territory", {"Alaska","Greenland","Alberta","Ontario"}},
    {"Greenland",{"Northwest Territory","Ontario","Eastern Canada","Iceland"}},
    {"Alberta",{"Alaska","Northwest Territory","Ontario","Western United States"}},
    {"Ontario",{"Greenland","Eastern Canada","Eastern United States","Western United States","Alberta","Northwest Territory"}},
    {"Eastern Canada",{"Greenland","Ontario","Eastern United States"}},
    {"Eastern United States",{"Ontario","Eastern Canada","Western United States"}},
    {"Western United States",{"Alberta","Ontario","Eastern United States","Central America"}},
    {"Central America",{"Venezuela","Western United States","Eastern United States"}},

    //South America
    {"Venezuela",{"Brazil","Peru"}},
    {"Brazil",{"Venezuela","North Africa","Peru","Argentina"}},
    {"Peru",{"Venezuela","Brazil","Argentina"}},
    {"Argentina",{"Peru","Brazil"}},

    //Africa
    {"North Africa",{"Western Europe","Egypt","East Africa","Central America"}},
    {"Egypt",{"Southern Europe","Middle East","North Africa","East Africa"}},
    {"East Africa",{"Middle East","Egypt","North Africa","Central Africa","South Africa","Madagascar"}},
    {"Central Africa",{"North Africa","East Africa","South Africa"}},
    {"South Africa",{"Central Africa","East Africa","Madagascar"}},
    {"Madagascar",{"East Africa","South Africa"}},

    //Australia
    {"Indonesia",{"Southeast Asia","New Guinea","Western Australia"}},
    {"New Guinea",{"Indonesia","Eastern Australia"}},
    {"Eastern Australia",{"New Guinea","Western Australia"}},
    {"Western Australia",{"Indonesia","Eastern Australia"}},

    //Asia
    {"Middle East",{"Southern Europe","Russia","Egypt","East Africa","India","Afghanistan"}},
    {"Afghanistan",{"Russia","Ural","China","India","Middle East"}},
    {"India",{"Middle East","Afghanistan","China","Southeast Asia"}},
    {"Southeast Asia",{"India","China","Indonesia"}},
    {"China",{"Southeast Asia","India","Afghanistan","Ural","Siberia","Mongolia"}},
    {"Ural",{"Russia","Afghanistan","China","Siberia"}},
    {"Siberia",{"Ural","China","Mongolia","Irkutsk","Yakutsk"}},
    {"Mongolia",{"China","Siberia","Irkutsk","Kamchatka","Japan"}},
    {"Japan",{"Mongolia","Kamchatka"}},
    {"Irkutsk",{"Mongolia","Siberia","Yakutsk","Kamchatka"}},
    {"Yakutsk",{"Siberia","Irkutsk","Kamchatka"}},
    {"Kamchatka",{"Yakutsk","Irkutsk","Mongolia","Japan","Alaska"}},

    //Europe
    {"Iceland",{"Greenland","Great Britain","Scandinavia"}},
    {"Great Britain",{"Iceland","Scandinavia","Northern Europe","Western Europe"}},
    {"Western Europe",{"North Africa","Southern Europe","Northern Europe","Great Britain"}},
    {"Southern Europe",{"Egypt","North Africa","Middle East","Russia","Northern Europe","Western Europe"}},
    {"Northern Europe",{"Russia","Southern Europe","Western Europe","Great Britain","Scandinavia"}},
    {"Scandinavia",{"Iceland","Great Britain","Northern Europe","Russia"}},
    {"Russia",{"Scandinavia","Northern Europe","Southern Europe","Middle East","Afghanistan","Ural"}}
};


};


#endif /* Territory_hpp */

And then my .cpp file:

//  Territory represents each individual territory on the map

#include <algorithm>
#include "Territory.hpp"
#include "Unit.hpp"
std::ostream& operator<<(std::ostream& os,const Territory& toPrint)
{
    os << toPrint.territoryName << " (" << toPrint.continent << ") " << "which is owned by " << toPrint.currentOwner << " with " << toPrint.numSingleUnitCount << " infantry in the territory." << std::endl;

    return os;
}

Territory::Territory(const string& terrName,const string& cont,const string& currOwner): territoryName(terrName), continent(cont), currentOwner(currOwner)
{
    numSingleUnitCount = 0;     //will be updated as new troops are added and removed... yak yak
    unitsInTerritory = std::vector<Unit*>();
}

Territory::~Territory()
{
    CLEAR UP SOME STUFF HERE.
}

string Territory::getTerritoryName() const
{
    return territoryName;
}

string Territory::getContinent() const
{
    return continent;
}

string Territory::getCurrentOwner() const
{
    return currentOwner;
}

void Territory::setCurrentOwner(const string& newOwner)
{
    if (currentOwner.compare(newOwner) != 0)
    {
        //means that the newOwner and the currentOwner are the same
        //which means that we can avoid self-assignment
        currentOwner = newOwner;
    }
}

int Territory::getNumUnitsInTerritory() const
{
    //returns the number of single infantry units
    //in this given territory
    return numSingleUnitCount;
}

bool Territory::borders(const Territory& anotherTerr) const
{
    //returns true if the two territories border each other
    //on the RISK gameboard.
    //

    auto iterator = COUNTRIES_BORDERING.find(territoryName);
    if (iterator != COUNTRIES_BORDERING.end())
    {
        //the current territory that "this" object represents
        //is in COUNTRIES_BORDERING.
        if (std::find( (iterator->second).cbegin(), (iterator->second).cend(),  anotherTerr.territoryName) != iterator->second.cend())
        {
            //anotherTerr is a neighbor of the current territory
            //as shown by the COUNTRIES_BORDERING map.
            return true;
        }
        else
        {
            //anotherTerr is not a neighbor of the current territory
            //as shown by the COUNTRIES_BORDERING map
            return false;
        }
    }
    else
        //the current territory that "this" object represents
        //is not in COUNTRIES_BORDERING. can not check for territory bordering.
        return false;
}

Unit* Territory::removeUnit(Unit* toRemove)
{
    //removes a unit from this territory
    //and returns a pointer to the removed unit
    Unit* tempPtr;
    for (int i = 0; i < unitsInTerritory.size(); i++)
    {
        tempPtr = unitsInTerritory[i];

        if (tempPtr == toRemove)
        {
            //temp and toRemove represent the same unit in the computer's memory,
            //so need to remove temp from the vector
            unitsInTerritory.erase(unitsInTerritory.begin() + i);

            //subtract the corresponding number of infantry "single" units from
            //the numSingleUnitCount
            numSingleUnitCount -= tempPtr->getUnitStrength();

            return tempPtr;
        }

    }

    //nothing was deleted
    return NULL;
}

void Territory::takeInUnit(Unit* toAddPtr)
{
    //adds a pointer to the unit to add to the vector
    unitsInTerritory.push_back(toAddPtr);
    numSingleUnitCount += toAddPtr->getUnitStrength();
}

Now, in my .cpp file, on the line of Territory::~Territory(), the compiler says "Exception specification declaration does not match previous declaration."

All the daniweb and stackoverflow posts I saw said that the error message involved erroneous exception specifications (throw...catch) but I did not do anything of that sort here.

Could anyone please shed some light on possibly why this message is eing displayed? Thank you very much in advance.

No clue on the destructor error, but I get linker errors if I keep ALL_BOARD_TERRITORIES and COUNTRIES_BORDERING initialization in Territory.hpp. I stuck them in Territory.cpp. I also had to stick std::vector<string> in the ALL_BOARD_TERRITORIES initialization 6 times (i.e. once per vector. Array has 6 vectors. 6 times). See below. This is moved from Territory.hpp to Territory.cpp.

I also have the below in my Territory.cpp and Unit.cpp files (note: cpp files, not hpp files. Forward declarations seem fine in the hpp files in lieu of the #include, to refer to your other thread. But in the .cpp files, I had a #include for both:

#include "Unit.hpp"
#include "Territory.hpp"

Not sure what's going on regarding your destructor. Make sure you get rid of any others first and see if that exception warning/error still comes up.

And again, consider moving the initialization of the below from Territory.hpp to Territory.cpp. I got linker problems with them remaining in Territory.hpp.

//represents all the territories on the map
const std::array<std::vector<string>,6> Territory::ALL_BOARD_TERRITORIES =
{
std::vector<string>    {   //NORTH AMERICA
        "Alaska","Northwest Territory","Greenland","Alberta","Ontario","Eastern Canada","Western United States","Eastern United States","Central America"

    },
std::vector<string>    {   //SOUTH AMERICA
        "Venezuela","Brazil","Peru","Argentina"

    },
std::vector<string>    {   //AFRICA
        "North Africa","Egypt","East Africa","Central Africa","South Africa","Madagascar"
    },
std::vector<string>    {   //AUSTRALIA
        "Indonesia","New Guinea","Western Australia","Eastern Australia"
    },
std::vector<string>    {   //ASIA
        "Middle East","Afghanistan","India","Southeast Asia","China","Mongolia","Japan","Kamchatka","Irkutsk","Yakutsk","Siberia","Ural"
    },
std::vector<string>    {   //EUROPE
        "Russia","Scandinavia","Iceland","Great Britain","Western Europe","Northern Europe","Southern Europe"
    }
};


//represents each territory and what other territories border it
const std::map<std::string,std::vector<std::string>> Territory::COUNTRIES_BORDERING =
{
    //format of the values in the map is: <<COUNTRY>,{Countries that border}>


    //North America
    { "Alaska", {"Kamchatka","Northwest Territory","Alberta"}},
    { "Northwest Territory", {"Alaska","Greenland","Alberta","Ontario"}},
    {"Greenland",{"Northwest Territory","Ontario","Eastern Canada","Iceland"}},
    {"Alberta",{"Alaska","Northwest Territory","Ontario","Western United States"}},
    {"Ontario",{"Greenland","Eastern Canada","Eastern United States","Western United States","Alberta","Northwest Territory"}},
    {"Eastern Canada",{"Greenland","Ontario","Eastern United States"}},
    {"Eastern United States",{"Ontario","Eastern Canada","Western United States"}},
    {"Western United States",{"Alberta","Ontario","Eastern United States","Central America"}},
    {"Central America",{"Venezuela","Western United States","Eastern United States"}},

    //South America
    {"Venezuela",{"Brazil","Peru"}},
    {"Brazil",{"Venezuela","North Africa","Peru","Argentina"}},
    {"Peru",{"Venezuela","Brazil","Argentina"}},
    {"Argentina",{"Peru","Brazil"}},

    //Africa
    {"North Africa",{"Western Europe","Egypt","East Africa","Central America"}},
    {"Egypt",{"Southern Europe","Middle East","North Africa","East Africa"}},
    {"East Africa",{"Middle East","Egypt","North Africa","Central Africa","South Africa","Madagascar"}},
    {"Central Africa",{"North Africa","East Africa","South Africa"}},
    {"South Africa",{"Central Africa","East Africa","Madagascar"}},
    {"Madagascar",{"East Africa","South Africa"}},

    //Australia
    {"Indonesia",{"Southeast Asia","New Guinea","Western Australia"}},
    {"New Guinea",{"Indonesia","Eastern Australia"}},
    {"Eastern Australia",{"New Guinea","Western Australia"}},
    {"Western Australia",{"Indonesia","Eastern Australia"}},

    //Asia
    {"Middle East",{"Southern Europe","Russia","Egypt","East Africa","India","Afghanistan"}},
    {"Afghanistan",{"Russia","Ural","China","India","Middle East"}},
    {"India",{"Middle East","Afghanistan","China","Southeast Asia"}},
    {"Southeast Asia",{"India","China","Indonesia"}},
    {"China",{"Southeast Asia","India","Afghanistan","Ural","Siberia","Mongolia"}},
    {"Ural",{"Russia","Afghanistan","China","Siberia"}},
    {"Siberia",{"Ural","China","Mongolia","Irkutsk","Yakutsk"}},
    {"Mongolia",{"China","Siberia","Irkutsk","Kamchatka","Japan"}},
    {"Japan",{"Mongolia","Kamchatka"}},
    {"Irkutsk",{"Mongolia","Siberia","Yakutsk","Kamchatka"}},
    {"Yakutsk",{"Siberia","Irkutsk","Kamchatka"}},
    {"Kamchatka",{"Yakutsk","Irkutsk","Mongolia","Japan","Alaska"}},

    //Europe
    {"Iceland",{"Greenland","Great Britain","Scandinavia"}},
    {"Great Britain",{"Iceland","Scandinavia","Northern Europe","Western Europe"}},
    {"Western Europe",{"North Africa","Southern Europe","Northern Europe","Great Britain"}},
    {"Southern Europe",{"Egypt","North Africa","Middle East","Russia","Northern Europe","Western Europe"}},
    {"Northern Europe",{"Russia","Southern Europe","Western Europe","Great Britain","Scandinavia"}},
    {"Scandinavia",{"Iceland","Great Britain","Northern Europe","Russia"}},
    {"Russia",{"Scandinavia","Northern Europe","Southern Europe","Middle East","Afghanistan","Ural"}}
};

You don't show the code in your Territory destructor. Are you throwing an exception there? If so, then you need to declare that in the header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.