Hi, I was trying to create a program where you create a person using a "Human" class. But I am getting long compiler problems. Can someone find the problem?
I still didn't understand how to overload the operator, but that is ok. I changed the code so that I don't need to oveload it (I think). This is what the code looks like now:
#include "StdAfx.h"
#include <iostream>
#include <string>
usingnamespace std;
class Human
{
private:
string firstName;
string lastName;
char gender;
int age;
public:
Human(string fName = "", string lName = "", char sexe = 'f', int pAge = 0)
{
firstName = fName;
lastName = lName;
gender = sexe;
age = pAge;
}
int introducePerson()
{
string extra = "";
if(gender=='f')
cout << "She ";
elseif (gender=='m')
cout << "He ";
else
cout << "The person ";
cout << "is ";
if(firstName != "" || lastName != "")
{
cout << "called ";
extra = "and is ";
if(firstName != "")
cout << firstName << " ";
if(lastName != "")
cout << lastName << " ";
}
cout << extra << age << " years old.";
return 0;
}
};
int main ()
{
char gen;
string fn, ln;
int year;
cout << "Description of a person:\n";
gender:
cout << "Is it a man or woman (m / f)? ";
cin >> gen;
if(gen=='m')
cout << "What is his first name? ";
elseif(gen=='f')
cout << "What is her first name? ";
else
{
cout << "Invalid input.\n";
goto gender;
}
cin >> fn;
if(gen=='m')
cout << "What is his last name? ";
elseif(gen=='f')
cout << "What is her last name? ";
cin >> ln;
if(gen=='m')
cout << "What is his age? ";
elseif(gen=='f')
cout << "What is her age? ";
cin >> year;
Human Person(fn, ln, gen, year);
Person.introducePerson();
return 0;
}
The problem is that I am still getting a compile error. It says "fatal error LNK1120: 1 unresolved externals". Do I still need to overload it, or is something else wrong?
There should be additional errors telling you which externals are unresolved. Given that the code above is fine, I'd guess that you're not actually compiling what you think you're compiling, or that you've picked the wrong kind of project in your IDE.
It does help. You've picked the wrong kind of project in your IDE. You told it you were making a Windows application when you're actually making a plain console application.
I just relised that none of the programs I have created, even the ones that worked in the past are working, and they are all getting the same error message. Is there anything I might have done to make it do this?
#include<iostream>
#include<string>
class Restaurant
{
private:
string restaurant_name;
int num_waiters;
int waiter_quality;
int num_tables;
int food_quality;
public:
restaurant();
~restaurant();
};
int main()
{
Restaurant MyRestaurant;
return 0;
}
and it has the same error message. I also realised that only my projects with classes in them have this error message. Can someone please find th problem?
Suspiciously weird. As if the name of the project and the name of the source file were conflated.
The problem is that your project is messed up. That the error message is a linker error, even though your most recent code above should not compile, indicates that whatever is being compiled is not the code you think is being compiled. Your problem is not a C++ problem; it's a messed up tools problem.