简体   繁体   中英

Function must return a value

I am trying to make a text based RPG and i'm fairly new to c++. I understand that I need to return a value, but when I try and return CharacterName or CharacterRace it comes up with unresolved externals errors. I'd really appreciate the help guys, thanks :)

CharacterCreation.h

#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

Problem is that the function CharacterCreation() (taking no arguments) is never defined, and thus the linker cannot find it.

Try substituting in the following:

std::string CharacterCreation(int, int);

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

This will call the CharacterCreation function you have implemented below the main function. Doing this I can compile (and link) your code :)

The "unresolved externals" message isn't directly caused by your returning a value.
It's a linker error, and only occurs because compilation succeeded.

The cause is that you're declaring, and calling, this parameter-less function:

std::string CharacterCreation();

but you're defining this function with two parameters:

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

The declaration and the definition must match.

From the looks of it, you don't actually want the parameters and should use local variables instead:

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

As I have pointed in my comment before, your CharacterCreation method does not return any value, although you have defined a string as an expected one.

What you most likely want to do is either change CharacterCreation signature to:

void CharacterCreation(int RaceChoice, int RaceChoiceLoop)

and keep the current implementation

or pack all your console output in a string and return it at the end of the method.

Then in main()

string result = CharacterCreation(); 

can retrieve this value and you can print it in main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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