I’m getting this huge linker error whenever I try to run these files.
I have a setup of my main code (main.cpp), my header file (Functions.hpp) and my function definition file (Functions.cpp).
I have made sure that both my main.cpp and Functions.cpp both have the #include "Functions.hpp" at the top.
Main.cpp
#include <iostream>
#include <fstream>
#include "Functions.hpp"
int main() {
std::cout << "Would you like to sign up? (y/n): ";
std::cin >> signuptrue;
if (signuptrue == "y" || signuptrue == "Y" || signuptrue == "Yes" || signuptrue == "yes") {
IsSignUp = true;
} else if (signuptrue == "n" || signuptrue == "N" || signuptrue == "No" || signuptrue == "no") {
IsSignUp = false;
} else {
std::cout << "Invalid entry\n";
}
if (IsSignUp) {
signup();
}
goodLogin = login();
if (goodLogin) {
std::cout << "You are logged in\n";
mainMenu();
} else {
std::cout << "You are not logged in, please try again.";
}
}
Functions.hpp
#include <iostream>
void signup();
bool login();
void mainMenu();
int findGold();
int updateGold(int newgold);
std::string SignUpUsername;
std::string SignUpPassword;
std::string Entered_username;
std::string Entered_password;
std::string Entered_login;
std::string signuptrue;
bool goodLogin = false;
bool IsSignUp = false;
int gold;
int newgold = 0;
int action = 0;
std::hash<std::string> hasher;
std::size_t hashedInput
Functions.cpp
#include <iostream>
#include <fstream>
#include "Functions.hpp"
void signup() {
std::string EnteredLogin;
std::cout << "Enter a username: ";
std::cin >> SignUpUsername;
std::cout << "Enter a password: ";
std::cin >> SignUpPassword;
std::cout << "You have signed up successfully\n";
std::string combinedInput = SignUpUsername + SignUpPassword;
std::size_t hashedInput = hasher(combinedInput);
std::fstream accountFile;
accountFile.open ("accounts.txt", std::ios::app);
accountFile << hashedInput << '\n';
accountFile.close();
}
bool login() {
std::string line;
std::cout << "Enter your username: ";
std::cin >> Entered_username;
std::cout << "Enter your password: ";
std::cin >> Entered_password;
std::string combinedInput = Entered_username + Entered_password;
hashedInput = hasher(combinedInput);
std::ifstream accountFile;
accountFile.open("accounts.txt");
while (getline(accountFile, line)) {
std::size_t hashedFileInput = std::stoul(line);
if (hashedInput == hashedFileInput) {
return true;
}
}
return false;
}
int findGold() {
std::ifstream accountFile("accounts.txt");
std::string line, goldAmountStr;
while (getline(accountFile, line)) {
if (line == std::to_string(hashedInput)) {
std::getline(accountFile, goldAmountStr);
try {
int goldAmount = std::stoi(goldAmountStr);
return goldAmount;
} catch(const std::invalid_argument& e) {
return 0;
}
}
}
accountFile.close();
return 0;
}
int updateGold(int newGold) {
std::ifstream inFile("accounts.txt");
std::ofstream outFile("temp.txt");
std::string line, goldAmountStr;
while (std::getline(inFile, line)) {
if (line == std::to_string(hashedInput)) {
std::getline(inFile, goldAmountStr);
outFile << line << '\n' << newGold << '\n';
} else {
outFile << line << '\n';
}
}
inFile.close();
outFile.close();
std::remove("accounts.txt");
std::rename("temp.txt", "accounts.txt");
return newGold;
}
void mainMenu() {
gold = findGold();
int ne;
std::cin >> ne;
gold = updateGold(ne);
std::cout << "you have " << gold << " gold";
}
I was researching on this and all it said was to make sure that I have included the header file in both the main.cpp and functions.cpp file.
I have done that but I still get the huge linker error.
g++ main.cpp Functions.hpp Functions.cpp
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0xc0): multiple definition of `SignUpUsername[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0xc0): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0xa0): multiple definition of `SignUpPassword[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0xa0): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x80): multiple definition of `Entered_username[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0x80): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x60): multiple definition of `Entered_password[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0x60): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x0): multiple definition of `hashedInput'; /tmp/ccpCKPCg.o:(.bss+0x0): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x14): multiple definition of `gold'; /tmp/ccpCKPCg.o:(.bss+0x14): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x40): multiple definition of `Entered_login[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0x40): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x20): multiple definition of `signuptrue[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0x20): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x8): multiple definition of `hasher[abi:cxx11]'; /tmp/ccpCKPCg.o:(.bss+0x8): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0xc): multiple definition of `action'; /tmp/ccpCKPCg.o:(.bss+0xc): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x10): multiple definition of `newgold'; /tmp/ccpCKPCg.o:(.bss+0x10): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x18): multiple definition of `IsSignUp'; /tmp/ccpCKPCg.o:(.bss+0x18): first defined here
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccA63Jrg.o:(.bss+0x19): multiple definition of `goodLogin'; /tmp/ccpCKPCg.o:(.bss+0x19): first defined here
collect2: error: ld returned 1 exit status
inlineproduces the program behaviour you need and didn't merely transform the problem form an obvious build-time error into a logic mistake you need to debug.