c++ - 在头文件中声明的函数给出了不在范围内的错误
问题描述
我正在开发一个没有全局定义变量、类对象或结构的项目。我在 Voter.cpp 的主文件中定义了一堆变量,并在 VoterDB.cpp 中定义了不同的数据库函数,并在 VoterDB.h 中进行了声明
VoterDB.h ---> VoterDB.cpp | | | 选民.cpp
选民.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"){
}
}
}
选民数据库.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;
}
选民数据库.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
生成文件
##
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_* 的不同组合,但到目前为止,这些都没有解决问题。
编辑:我将函数调用更改为 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)
解决方案
正如 NathanOliver 所说,这个答案应该作为错字删除。我写它只是为了澄清你,因为你要求它。
要调用类的静态成员函数,您必须提供类的名称。
所以,在 main() 中,而不是
cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
利用
VoterDB::cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
推荐阅读
- active-directory - Ldap 搜索不返回 Active Directory 中帐户的所有属性
- python-3.x - 将zip文件本地下载到tempfile,将文件解压到tempfile,并列出文件
- regex - 是否可以从golang中的字符串(复杂字符串)中检索子字符串
- hadoop - MapReduce Reducer 的 KeyOut 类型
- swift - 如何在stackview上设置约束
- c# - 如何在 WPF 中将 datagrid 与两个 itemsSource 绑定?
- jquery - 使用 Jquery 添加复选框值
- java - Spring没有在Mysql中保存数据
- python - 读取并关闭一组文件
- javascript - Javascript - 从末尾开始并移动的子字符串