头文件中声明的函数未出现作用域错误

时间:2018-09-19 15:41:11

标签: c++ scope header

我正在一个没有全局定义变量,类对象或结构的项目中工作。我在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_ *函数声明为静态函数以及将VodDB :: 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)

1 个答案:

答案 0 :(得分:0)

正如NathanOliver所说,此答案应作为错字删除。我写这个只是为了澄清您的意思,因为您要求这样做。


要调用类的静态成员函数,必须提供该类的名称。

因此,在main()中,而不是

cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);

使用

VoterDB::cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);