繁体   English   中英

函数必须返回一个值

[英]Function must return a value

我正在尝试制作基于文本的RPG,而我对C ++还是相当陌生。 我知道我需要返回一个值,但是当我尝试返回CharacterName或CharacterRace时,它会出现无法解决的外部错误。 我非常感谢帮助人员,谢谢:)

角色创建

#include <string>
#include <iostream>

void petc(), ConsoleClear(), petc(), EnterClear();

std::string CharacterName, CharacterRace;

Main.cpp

#include <iostream>
#include <limits>
#include <string>
#include <string.h>
#include "CharacterCreation.h"

std::string CharacterCreation();


int main()
{
    CharacterCreation();

}



std::string CharacterCreation(int RaceChoice, int RaceChoiceLoop)
{

RaceChoiceLoop = 0;
std::cout << "Welcome to the character creation V 1.0.0" << std::endl;
EnterClear();
std::cout << "Choose a name: ";
std::cin >> CharacterName;
std::cout << CharacterName << std::endl;

EnterClear();

while (RaceChoiceLoop == 0)
{

    std::cout << "(1) Human - Human's race perks: + 5 to Magic | + 1 to         Sword Skill" << std::endl;
    std::cout << "(2) Elf - Elve's race perks: + 5 to Archery | + 1 to Magic" << std::endl;
    std::cout << "(3) Dwarf - Dwarven race perks: + 5 to Strength | + 1 to Archery" << std::endl;
    std::cout << "Choose a race, " << CharacterName << ": ";
    std::cin >> RaceChoice;

    if (RaceChoice == 1)
    {
        RaceChoiceLoop = 1;
        CharacterRace = "Human";
    }

    else if (RaceChoice == 2)
    {
        RaceChoiceLoop = 1;
        CharacterRace = "Elf";
    }

    else if (RaceChoice == 3)
    {
        RaceChoiceLoop = 1;
        CharacterRace = "Dwarf";
    }

    else
    {
        std::cout << "Invalid Option";
        EnterClear();
        RaceChoiceLoop = 0;

    }

}






}



void petc()
{
    std::cout << "Press Enter To Continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}




void EnterClear()
{
    std::cout << "Press Enter To Continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    system("cls");

}



void ConsoleClear()
{
    system("cls");
}

声明的std :: string函数应该返回一个字符串,这与在屏幕上打印该字符串不同,请在函数内部使用return“ something”否则将其声明为void

问题在于从未定义函数CharacterCreation() (不带参数),因此链接器无法找到它。

尝试替换以下内容:

std :: string CharacterCreation(int,int);

int main(){CharacterCreation(1,1); }

这将调用您在main函数下方实现的CharacterCreation函数。 这样做,我可以编译(和链接)您的代码了:)

“未解决的外部因素”消息不是直接由您返回值引起的。
这是一个链接器错误,仅在编译成功后才会发生。

原因是您要声明并调用此无参数函数:

std::string CharacterCreation();

但是您要使用两个参数定义此函数:

std::string CharacterCreation(int RaceChoice, int RaceChoiceLoop)

声明和定义必须匹配。

从外观上看,您实际上并不需要参数,而应该使用局部变量:

std::string CharacterCreation()
{
    int RaceChoice = 0;
    int RaceChoiceLoop = 0;
    // ...

正如我之前在评论中指出的那样,尽管您已将字符串定义为期望的字符串,但您的CharacterCreation方法不会返回任何值。

您最可能想做的是将CharacterCreation签名更改为:

void CharacterCreation(int RaceChoice, int RaceChoiceLoop)

并保持当前的实施

或将所有控制台输出打包为字符串,然后在方法末尾将其返回。

然后在main()

string result = CharacterCreation(); 

可以检索此值,然后可以在主窗口中打印

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM