0

When I run my project, I get this error:

XCODE ERROR

This code was working before I added the functionality to read CSV file data. Here is the Github link to my project for whoever wants to look at my complete code: Github Link

Here is the main.ccp code:

#include "Characters.hpp"
#include "Enemies.hpp"
#include <fstream>

vector<Enemy> LoadEnemies(string filename){
    ifstream file(filename);
    vector<Enemy> Enemies;
    
    int i = 0;
    for(string line; getline(file, line); ){
        
        if (i == 0){
            i++;
            continue;
        }
        Enemy temp = Enemy();
        temp.loadfromCSV(line);
        Enemies.push_back(temp);
    }
    file.close();
    return Enemies;
}

int main() {
    Character c = Character("Alger Waterlow");
    cout << c << endl;
    
    vector<Enemy> Enemies = LoadEnemies("Enemies.csv");
    
    for(auto E : Enemies){
        cout << E << endl;
    }
    
    

    return 0;
}

Here is the Characters Header File:

#ifndef Characters_hpp
#define Characters_hpp

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Character {
private:
    string name_;
    //string type_;
    int hp_;
    int str_;
    int dex_;
    int int_;
    int def_;
    int lvl_;

public:
    Character() : name_{"Stanger"}, hp_{5}, str_{2}, dex_{2}, int_{2}, def_{2}, lvl_{1} {
        cout << "Default Character Created!" << endl;
    }
    Character(string name) : name_{name} {
        hp_ = 5;
        str_ = 2;
        dex_ = 2;
        int_ = 2;
        def_ = 2;
        lvl_ = 1;
        cout << "Default Attributes Given To " << name << endl;
    }

    auto & name() {return name_;}
    void setname(string name) {name_ = name;}
    auto & health() {return hp_;}
    void sethealth(int hp) {hp_ = hp;}
    auto & strength() {return str_;}
    void setstrength(int str) {str_ = str;}
    auto & dexterity() {return dex_;}
    void setdexterity(int dex) {dex_ = dex;}
    auto & intelligence() {return int_;}
    void setintelligence(int intel) {int_ = intel;}
    auto & defense() {return def_;}
    void setdefense(int def) {def_ = def;}
    auto & level() {return lvl_;}
    void setlevel(int lvl) {lvl_ = lvl;}




    friend ostream& operator<<(ostream& os, Character& c){
        return os << c.name() << endl << "  "
        << "Strength: " << c.strength() << endl << "   "
        << "Dexterity: " << c.dexterity() << endl << "  "
        << "Intelligence: " << c.intelligence() << endl << "  "
        << "Defense: " << c.defense() << endl;
    }
};
#endif

And here is the Enemies Header File:

#ifndef Enemies_hpp
#define Enemies_hpp

#include "Characters.hpp"

class Enemy : public Character {
private:
    int exp_;
public:
    Enemy() : Character(), exp_{10}{}
    auto & exp() {return exp_;}
    void setexp(int exp) {exp_ = exp;}
    
    friend ostream& operator<<(ostream& os, Enemy& c){
        return os << c.name() << endl << "  "
        << "Strength: " << c.strength() << endl << "   "
        << "Dexterity: " << c.dexterity() << endl << "  "
        << "Intelligence: " << c.intelligence() << endl << "  "
        << "Defense: " << c.defense() << endl << "  "
        << "Exp: " << c.exp() << endl;
    }
    void loadfromCSV(string line);
};

#endif

The Enemies.cpp file simply has the code to read the Enemies.csv file data:

#include "Enemies.hpp"
#include "Utils.cpp"

void Enemy::loadfromCSV(string line){
    vector<string> tokens = tokenizecsvline(line);
    
    setname(tokens[0]);
    sethealth(stoi(tokens[1]));
    setstrength(stoi(tokens[2]));
    setdexterity(stoi(tokens[3]));
    setintelligence(stoi(tokens[4]));
    setdefense(stoi(tokens[5]));
    setlevel(stoi(tokens[6]));
    setexp(stoi(tokens[7]));
}

And the utils file simply tokenizes the csv line:

vector<string> tokenizecsvline(string line){
    vector<string> token;
    stringstream linestream(line);
    string temp;
    
    while(getline(linestream, temp, ',')){
        token.push_back(temp);
    }
    return token;
}

Any help is appreciated! Thank you!(New to programming as you can probably tell).

273K
  • 19,191
  • 8
  • 34
  • 47
  • 2
    `#include "Utils.cpp"` is a problem. The contents of the file will be included and compiled in Enemies.cpp as well as in Utils.cpp. You'd usually want to declare the function in a header file and include that where you need to be able to call the function. The definition, or code, of the function would be in a cpp file that is compiled and linked with your program. – Retired Ninja Dec 31 '21 at 22:54

0 Answers0