I have tried so many things and I am stumped. I keep getting a redefinition error and cannot for the life of me understand what the problem is.
This is making me lose my mind so some help would be greatly appreciated 🙂
The compilation commands I am using:
g++ -o TESTFM.o -c FileManagement/filemanager.h
g++ -o TESTEX.o main.cpp TESTFM.o
main.cpp
#include "FileManagement/filemanager.h"
#include <iostream>
using namespace std;
int main () {
Config::loadFromFile ("test.config");
cout << Config::regionFile << endl;
cout << Config::maxTime << endl;
cout << Config::refreshRt << endl;
RegionLayout::loadFromFile ();
cout << RegionLayout::getCell (2, 2) << endl;
cout << RegionLayout::x << " x " << RegionLayout::y << endl;
return 0;
}
FileManagement/filemanager.h
#ifndef FILEMANAGER_H
#define FILEMANAGER_H
#pragma once
#include <vector>
#include <string>
class City;
namespace Config {
std::string regionFile; // relative path to file containing region layout
int maxTime; // maximum number of time steps
int refreshRt; // refresh rate (i.e. output every n time steps)
void loadFromFile (std::string path); // loads config from the given path
};
namespace RegionLayout {
int x, y; // stores size of initial region layout
char layout[16][16]; // stores double array of initial layout
char getCell (int x, int y); // get initial layout cell at position (x, y)
void loadFromFile (); // loads layout from file at path Config.regionFile
City generateCity (); // returns a City generated from initial layout (raises error if data not loaded from file)
};
namespace OutputManager {
std::vector<std::string> outputLines; // stores all output lines
void commitLine (std::string line); // adds line to outputLines
void commitToFile (std::string path); // writes to file at path
};
#endif
FileManagement/filemanager.cpp
#ifndef FILEMANAGER_CPP
#define FILEMANAGER_CPP
#include "filemanager.h"
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void Config::loadFromFile (string path) {
ifstream file;
file.open (path);
string tmp;
getline (file, Config::regionFile);
getline (file, tmp);
Config::maxTime = stoi (tmp);
getline (file, tmp);
Config::refreshRt = stoi (tmp);
file.close ();
}
char RegionLayout::getCell (int x, int y) {
return RegionLayout::layout[x][y];
}
void RegionLayout::loadFromFile () {
int x = 0;
int y = 0;
int width = 0;
ifstream file;
file.open (Config::regionFile);
string tmp, tmp2;
while (getline (file, tmp)) {
// cout << "Line " << tmp << endl;
x = 0;
istringstream iss (tmp);
while (getline (iss, tmp2, ',')) {
// cout << "Char '" << tmp2 << "'" << endl;
RegionLayout::layout[x][y] = tmp2[0];
x++;
}
if (x > width) width = x;
y++;
}
RegionLayout::x = width;
RegionLayout::y = y;
}
#endif
New contributor
Uriel Hansen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.