Я работаю над проектом, в котором нет глобально определенных переменных, объектов класса или структур.У меня есть набор переменных, определенных в основной части Voter.cpp, и различные функции базы данных, определенные в VoterDB.cpp, с объявлениями, сделанными в VoterDB.h
VoterDB.h ---> VoterDB.cpp |||Voter.cpp
Voter.cpp
#include <iostream>
#include <stdlib.h>
#include "VoterDB.h"
using namespace std;
class Voter {
public:
private:
};
int main(int argc, char *argv[])
{
string lastname="";
string firstname="";
int age=0;
int stnum=0;
string street="";
string town="";
string zip="";
float amtdonated=0;
cout << ">: ";
string command;
getline (cin, command);
while(command != "Quit"){
if(command=="New"){
cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
} else if(command=="Update"){
cmd_update(lastname, firstname, age, stnum,street,town,zip);
} else if(command=="View"){
cmd_view(lastname,firstname,age,stnum,street,town,zip,amtdonated)
} else if(command=="Donate"){
} else if(command=="Report"){
}
}
}
VoterDB.cpp
#
include < iostream > #include < stdlib.h > #include < string > #include "VoterDB.h"
using namespace std;
void cmd_new(string & lastname, string & firstname, int & age, int & stnum, string & streetname, string & town, string & zip, float & amtdonated) {
cout << "\nEnter First Name\n";
cout << ">: ";
getline(cin, firstname);
cout << "\nEnter Last Name\n";
cout << ">: ";
getline(cin, lastname);
cout << "\nEnter Age\n";
cout << ">: ";
stringstream(cin, age);
cout << "\nEnter Street Number\n";
cout << ">: ";
stringstream(cin, stnum);
cout << "\nEnter Street Name\n";
cout << ">: ";
getline(cin, streetname);
cout << "\nEnter Town\n";
cout << ">: ";
getline(cin, town);
cout << "\nEnter Zipcode\n";
cout << ">: ";
getline(cin, zip);
amtdonated = 0.0;
return;
}
void cmd_update(string & lastname, string & firstname, int & age, int & stnum, string & streetname, string & town, string & zip) {
string input = "";
cout << "\nEnter Y or N\n";
cout << "Change First Name? (" << firstname << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, firstname);
input = "";
}
cout << "\nChange Last Name? (" << lastname << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, lastname);
input = "";
}
cout << "\nChange Age? (" << age << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
stringstream(cin, age);
input = "";
}
cout << "\nChange Street Number? (" << stnum << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
stringstream(cin, stnum);
input = "";
}
cout << "\nChange Street Name? (" << streetname << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, streetname);
input = "";
}
cout << "\nChange Town? (" << town << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, town);
input = "";
}
cout << "\nChange Zipcode? (" << zip << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, zip);
input = "";
}
return;
}
void cmd_view(string & lastname, string & firstname, int & age, int & stnum, string & streetname, string & town, string & zip, float & amtdonated) {
cout << firstname << " " << lastname << ", " << age << "\n"
cout << stnum << " " << streetname << "\n"
cout << town << " " << zip << "\n"
cout << "Amount Donated: $" << amtdonated;
return;
}
VoterDB.h
#ifndef VOTERDB_H
# define VOTERDB_H
# include < iostream > #include < stdlib.h > #include < string >
using namespace std;
class VoterDB {
public:
static void cmd_new(string lastname, string firstname, int age, int stnum, string streetname, string town, string zip, float amtdonated);
static void cmd_update(string lastname, string firstname, int age, int stnum, string streetname, string town, string zip);
static void cmd_view(string lastname, string firstname, int age, int stnum, string streetname, string town, string zip, float amtdonated);
private:
};
#endif
makefile
##
Specifiy the target
all: Voter
# Specify the object files that the target depends on# Also specify the object files needed to create the executable
Voter: Voter.o VoterDB.o
g++Voter.o VoterDB.o - o Voter.exe
# Specify how the object files should be created from source files
VoterDB.o: VoterDB.cpp
g++ - c VoterDB.cpp
Voter.o: Voter.cpp
g++ - c Voter.cpp
# Specify the object files and executables that are generated# and need to be removed to re - compile the whole thing
clean:
rm - f * .o Voter.exe
Моя проблема в том, что всякий раз, когда я пытаюсь скомпилировать это, я получаю ошибку
$ make
g++ -c Voter.cpp
Voter.cpp: In function ‘int main(int, char**)’:
Voter.cpp:28:66: error: ‘cmd_new’ was not declared in this scope
cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
^
Voter.cpp:30:61: error: ‘cmd_update’ was not declared in this scope
cmd_update(lastname, firstname, age, stnum,street,town,zip);
^
Voter.cpp:32:67: error: ‘cmd_view’ was not declared in this scope
cmd_view(lastname,firstname,age,stnum,street,town,zip,amtdonated)
^
makefile:15: recipe for target 'Voter.o' failed
make: *** [Voter.o] Error 1
Я пробовал разные комбинации, объявляя различные функции cmd_ * как статические и как VoterDB :: cmd_ *но до сих пор ни одна из них не устранила проблему.
edit: я изменил вызовы функций на VoterDB :: cmd_ * и переключил функции со статического void на void.но теперь я получаю сообщение об ошибке
Voter.cpp:32:76: error: cannot call member function ‘void VoterDB::cmd_view(std::__cxx11::string, std::__cxx11::string, int, int, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string, float)’ without object
VoterDB::cmd_view(lastname,firstname,age,stnum,street,town,zip,amtdonated)