Can't find problem - long compilor error

Pages: 12
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "StdAfx.h"
#include <iostream>
#include <string>
using namespace std;

enum sexe
{
	m,
	f
};

class Human
{
private:
	string firstName;
	string lastName;
	char gender;
	int age;

public:
	Human(string fName = NULL, string lName = NULL, sexe sexe = f, int pAge = 0)
	{
		firstName = fName;
		lastName = lName;
		gender = sexe;
		age = pAge;
	}
	int introducePerson()
	{
		string and = "";
		if(gender==f)
			cout << "She ";
		else if (gender==m)
			cout << "He ";
		else
			cout << "The person ";
		
		cout << "is ";
		if(firstName != "" || lastName != "")
		{
			cout << "called ";
			and = "and is ";
			if(firstName != "")
				cout << firstName << " ";
			if(lastName != "")
				cout << lastName << " ";
		}
		cout << and << age << " years old.";
	}
}

int main ()
{
	sexe 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? ";
	else if(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? ";
	else if(gen==f)
		cout << "What is her last name? ";
	cin >> ln;
	if(gen==m)
		cout << "What is his age? ";
	else if(gen==f)
		cout << "What is her age? ";
	cin >> year;
	Human Jack(fn, ln, gen, year);
	Jack.introducePerson();
	return 0;
}
line 50: You have to put a semicolon at the end of a class definition.

line 30: and is a keyword so you can't have variables with that name.

introducePerson() doesn't return a value.

line 61: You will have to overload operator>> to make it work with sexe objects.
Thanks a lot for your help, but could you tell me how to overload the operator (I'm new to c++, that's why I'm on the beginner page)?
1
2
3
4
5
std::istream& operator>>(std::istream& is, sexe& s)
{
	// Read data from is and use it to set s to the correct value.
	return is;
}
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "StdAfx.h"
#include <iostream>
#include <string>
using namespace 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 ";
		else if (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? ";
	else if(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? ";
	else if(gen=='f')
		cout << "What is her last name? ";
	cin >> ln;
	if(gen=='m')
		cout << "What is his age? ";
	else if(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?
I don't know why you get that error message. Is that the whole error message?
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.
This is the exact error message I am getting:
1
2
3
4
5
1
1>  stdafx.cpp
1>  Project.cpp
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\Max\documents\visual studio 2010\Projects\Project\Debug\Project.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Does that help with finding the problem?
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.
Is there a way I can change the file from a windows application to the console application, or do I just create a new file and paste the code into it?
I created a new file that is a console application and pasted the code and am still getting this error message:
1
2
3
1
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>c:\users\max\documents\visual studio 2010\Projects\human.cpp\Debug\human.cpp.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Do you know what may be wrong?
A new file won't do. You need to make a new project or whatever your IDE calls them.

As an aside, this thread joins my collection of "reasons why beginners shouldn't use an IDE" threads :)
When I wrote file, I ment project: I created a new project and it still had an error message (the one from my 2:50pm post).
Did you definitely include the file with the code in the new project?
I copied and pasted the code into the new file, what file am I supposed to include.
Maybe you are creating the wrong type of project. I think it should be a "Win32 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?
I've created this new program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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?
^ You didn't provide the definitions of the constructor and destructor.
The error message should have told you that.

human.cpp.exe weird name...
human.cpp.exe weird name...


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.
Pages: 12