c++ - 即使我已经定义了所有功能,但在C++中仍获得对错误的 undefined reference

标签 c++ compiler-errors linker

enum state{
state_Mmenu,    // State 0
state_settingmenu,      // State 1
state_gamemenu,     // State 2
state_savegamemenu,     // State 3
state_loadgamemenu,     // State 4;
state_Exit,         // State 5;
state_other
};

state stateMmenu();
state statesettingmenu(string, string);
state stategamemenu();
state statesavegamemenu(string, string, string, ofstream&);
state stateloadgamemenu(string, string, string, ifstream&);
state stateExit();



int main()
{
state curr=state_other;

string filename= "PlayerOne.jak";
string fname="Player";
string lname="One";
ifstream inFS;
ofstream outFS;



while(curr != state_Exit)
{
    switch(curr)
    {
        case state_Mmenu:
        curr = stateMmenu();
        break;

        case state_settingmenu:
        curr = statesettingmenu(fname, lname);
        break;

        case state_gamemenu:
        curr = stategamemenu();
        break;

        case state_savegamemenu:
        curr = statesavegamemenu(fname, lname, filename, outFS);
        break;

        case state_loadgamemenu:
        curr = stateloadgamemenu(fname, lname, filename, inFS);
        break;

        default:
            cout << "Don't Tip the Vending Machine"<<endl;
            curr = state_Mmenu;
        break;
    }
}


return 0;

 }


state statesavegamemenu(string fname, string lname, string filename, 
ofstream outFS)
{
state next=state_savegamemenu;
int choice4=0;
cout<<" -Save Game Menu-----------------"<<endl;
cout<<"1. File Name - "<<filename<<endl;
cout<<"2. Save" <<endl;
cout<<"3. Game Menu" <<endl;
cout<<"----------------------------"<<endl;
    do
{

    cout << "Choice? "<< endl;
    cin >> choice4;
    cin.clear();
    cin.ignore(10000,'\n');

}while(!cin.good());

switch(choice4)
{
    case 1:
    {
        cout<<"What would you like to save this file as? ";
        cin>>filename;
        cout<<endl;
        next = state_savegamemenu;
    }
    break;
    case 2:
    {
        outFS.open(filename.c_str());
        if(!outFS.is_open())
        {
            next = state_savegamemenu;
            break;
        }
        else
        {
            outFS<<fname<<endl;
            outFS<<lname<<endl;
        }
        outFS.close();
        next = state_savegamemenu;
    }

    break;
    case 3:
        next = state_gamemenu;
    break;
    default:
    cout << "Bad Input!"<<endl;
        next = state_savegamemenu;
    break;  
}
cout <<endl;
return next;
}

state stateloadgamemenu(string fname, string lname, string filename, 
ifstream inFS)
{
state next=state_loadgamemenu;
int choice5=0;
cout<<" -Save Game Menu-----------------"<<endl;
cout<<"1. File Name - "<<filename<<endl;
cout<<"2. Load" <<endl;
cout<<"3. Main Menu" <<endl;
cout<<"----------------------------"<<endl;
string filecontent;
    do
{

    cout << "Choice? "<< endl;
    cin >> choice5;
    cin.clear();
    cin.ignore(10000,'\n');

}while(!cin.good());

switch(choice5)
{
    case 1:
    {
        cout<<"What file would you like to load? ";
        cin>>filename;
        cout<<endl;
        next = state_loadgamemenu;
    }
    break;
    case 2:
    {
        inFS.open(filename.c_str());
        if(!inFS.is_open())
        {
            next = state_savegamemenu;
            break;
        }
        else
        {
            while(!inFS.eof())
            {
                inFS >> filecontent;
                if(inFS.good())
                {
                    cout << filecontent << endl; 
                }
            }
        }
        inFS.close();
        next = state_loadgamemenu;
    }

    break;
    case 3:
        next = state_Mmenu;
    break;
    default:
    cout << "Bad Input!"<<endl;
        next = state_loadgamemenu;
    break;  
}
cout <<endl;
return next;
}

我得到的错误是对statesavegamemenu()和stateloadgamemenu()的 undefined reference 。据我所知,这样的链接器错误与程序无法找到相关功能的定义有关,但是我非常清楚地实现了它们,因此对于为什么会发生这种情况以及任何帮助我非常困惑将不胜感激。

最佳答案

在保存游戏和加载游戏中,您忘记了执行此操作,因此与 call 不匹配

 state statesavegamemenu(string fname, string lname, string filename, 
ofstream outFS)
{


 state statesavegamemenu(string fname, string lname, string filename, 
 ofstream& outFS)
 {  

与其他函数相同,但用ifstream代替ifstream&

关于c++ - 即使我已经定义了所有功能,但在C++中仍获得对错误的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577410/

相关文章:

c++ - 让 CMake 在可能的情况下选择静态链接?

c - Visual C++ 2015/GL(全程序优化)和/OPT :REF (optimize references) prevent static initializers being called

c++ - 在实现(.cpp)文件中不包括相应的头文件(.h)仍然编译?

c++ - UDP 数据是否会损坏?

c++ - 适用于 begin() 但不适用于 rbegin() 的映射迭代器

c# - SWIG、Box2D 和 C#

c - 是什么导致我的代码不确定?

excel - 错误: invalid or unqualified - VBA copy paste

c++ - 检索有关当前登录 session 的信息(Visual C++,Win 7)

包含自身映射的 C++ 结构