0

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");
}
Lil Jakers
  • 77
  • 1
  • 1
  • 4

4 Answers4

3

A declared std::string function should return a string and this is not the same as printing it on the screen, use return "something" inside the function otherwise declare it void.

drscaon
  • 401
  • 2
  • 9
2

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;
    // ...
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you so much, you were right I needed to make them local variables not parameters, lesson learn thank you very much for taking the time to help me out. – Lil Jakers Nov 18 '15 at 15:16
  • in the meanwhile the errors is: Error 1 error C4716: 'CharacterCreation' : must return a value c:\users\jake\documents\visual studio 2013\projects\characterselection\characterselection\main.cpp 72 :) so as long as this is an issue, it wasnt the exactly one mentioned here.. – mikus Nov 18 '15 at 15:16
  • @mikus Well, it *must* return a value because you said so when you stated its type. If you don't want it to return a value, change the return type from `std::string` to `void`. – molbdnilo Nov 18 '15 at 15:18
  • @molbdnilo I did change the data type of the function to void. – Lil Jakers Nov 18 '15 at 15:21
  • @molbdnilo, hah, you don't need to tell me, that's exactly what I explained in my answer and much earlier in the comment :P Im just saying that as long as the above is true, the error was about (no) return value :P – mikus Nov 18 '15 at 15:39
  • @mikus Yeah, I didn't look at the name on your comment. Sorry about that. – molbdnilo Nov 18 '15 at 15:40
0

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 :)

Banan
  • 434
  • 6
  • 12
  • Still receiving this error: Error 1 error C4716: 'CharacterCreation' : must return a value c:\users\jake\documents\visual studio 2013\projects\characterselection\characterselection\main.cpp 72 1 CharacterSelection – Lil Jakers Nov 18 '15 at 15:12
  • @LilJakers Thats another error. Put a `return` statement returning a `std::string` on the bottom of `CharacterCreation`. Probably its because your compiler settings are more restrictive than they should be, at least according to this [answer](http://stackoverflow.com/questions/1610030/why-can-you-return-from-a-non-void-function-without-returning-a-value-without-pr). – Banan Nov 18 '15 at 15:17
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

mikus
  • 3,042
  • 1
  • 30
  • 40