error LNK2001?

Pages: 12
Ok, so like I recently started learning C++ on my own. It said here about learning how to separate interface from implementation from the book so I did one of the programs. Here's my code:

GradeBook.h:
#include "stdafx.h"
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:

GradeBook( string ); // constructor that initializes courseName
void setCourseName( string ); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage();



private:
string courseName; // course name for this GradeBook
; };// end class GradeBook


ConsoleApplication9:


#include "stdafx.h"



#include <iostream>

#include "GradeBook.h"

using namespace std;

GradeBook::GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string GradeBook::getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()<< "!" << endl;
} // end function displayMessage


I am using Visual Studios 2012 and it had the error"error LNK2001: unresolved external symbol _mainCRTStartup". I've heard that you have to have a main function, so I did put a main function in the CPP but it showed nothing. What did I do wrong and how do I fix it? Thx
Did you make an empty project? It sounds like you used Visual Studio's Console Project or something.
I really don't know. I'm all learning this by myself and the book talks about separating the interface from the implementation, this was the code on the book. I ran it with visual studios with visual studios. http://www.cplusplus.com/forum/beginner/117700/ this was my other forum and the person who replied says that you have to have a main function, as I had added one, but it seems to be recognized as an empty project. How exactly am I supposed to change the settings to program using classes? thx.
When you create your project in VS, select Empty Project instead of CRTConsole or whatever you selected. You can also go into the project settings and there is an option somewhere that you'll want to set to say "Compile as Native C++" or something similar.
from your other post, your main() function definition is :
1
2
3
4
int main()
{
system("pause");
}


actually, you have no statements to do anything, just a plain system( "pause ") and the purpose of it is to actually pause the program and print
"Press any key to continue..."
That has nothing to do with the TC's problem. TC is getting linker errors because the program is set to be compiled as a CRT Console program or something so it isn't looking for main as the entry point, and rather the special CRTmain thing.
no, he actually compiled it and run it, as he say from his other post :

and when I compile&build it, it seems that it doesn't run anything but displaying"press any key to continue..." Does this mean I cannot program by separating the interface from implementation in Visual Studios? Thanks, because I am learning all by my own as a high school student
I'm really sorry... I don't understand what you guys are saying. So is it that you have to program using a main function in VS? Or is it that you can't program by class implementation? And what is TC, CRTConsole, and "Compile as Native C++? Thank you guys alot, because I really don't know what to do, I haven't programmed in a month because of this. Or should I just program using main and in client code? thx.
Allow me to re-iterate:

There are a couple of problems that are occurring:

1.) The way in which you create the project is causing problems.

2.) The way in which you use the main() function is causing problems.

In your situation, as a beginner, it is advisable to create "empty" projects. Whenever you create a new project, you are greeted with a dialogue menu, in which you can type in a name and change some other project parameters.

1.) You can choose to create a Win32 console application, or a Win32 project. Since you're not going to be working with the windows API extensively just yet, select Win32 console application.

2.) Check the "empty project" check box. Also un-check the "precompiled header" check box. This way, your project won't be dependent on that nasty "stdafx.h" pre-compiled header. It has it's own purpose, which you shouldn't have to worry about yet.

Congrats, now you have an empty win32 console project.
Now, you just need to create and add a main .cpp file, and write a main() function.

Judging by how you wrote your main() function in that other thread of yours, I assume that the concept of a main() function is mysterious to you(?)
If that's the case, then it would be hard for me to explain in this post. It would be easier for me to just redirect you to a beginner tutorial or something:
http://www.cplusplus.com/doc/tutorial/program_structure/
Thank you very much... I appreciate it very much, but I still do have questions. So what exactly is an empty project? And when I did create it, it seems like the "precompiled header" check box is grey. And when I do actually create a empty project, how do I write code? And from the tutorial, i've seen that you must have a main function, but why does the code in the book doesn't have a main function? Because most of the code illustrated in this forum is mostly copied directly from the book. Do I have to change the code into something like this:
GradeBook.h:
#include "stdafx.h"
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
}

void setCourseName( string name )
{
courseName = name; }

string getCourseName()
{
return courseName; // return object's courseName
}

void displayMessage()
{

cout << "Welcome to the grade book for\n" << getCourseName()<< "!" << endl;
}
private:
string courseName; // course name for this GradeBook
;

Cpp:
#include <iostream>
#include "stdafx.h"
#include <string>
int main()
{
GradeBook mygradebook("Electronics");

cout<<"Welcome to the grade book for\n"<<mygradebook.getCourseName()<< endl;
}

There may be many problems in this code, but my main question is, do you have to write code like this? Is the code in the book just a demonstration, waiting for you to write a main function by yourself? While writing this code I suddenly realized that in the first code I wrote/copied, I didn't use the constructor to initialize coursename. I am getting more and more confused and not even sure what I'm confused about. Please help?! A THOUSAND THANKS!
Ok that code had a bit too much mistakes... Here I write it again

GradeBook.h:
#include "stdafx.h"
#include <string>
#include <iostream>
// class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition

class GradeBook
{
public:
GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
}

void setCourseName( string name )
{
courseName = name; }

string getCourseName()
{
return courseName; // return object's courseName
}

void displayMessage()
{

cout << "Welcome to the grade book for\n" << getCourseName()<< "!" << endl;
}
private:
string courseName; // course name for this GradeBook
; };


Cpp:
#include "stdafx.h"
#include <iostream>
#include "GradeBook.h"

using namespace std;

int main()
{
GradeBook mygradebook("Electronics");

cout<<"Welcome to the grade book for\n"<<mygradebook.getCourseName()<< endl;
system("pause");
}
what exactly is an empty project?


Well, a non-empty project is a project which contains several skeletal files to start you off with. Usually these files will have a main() function, some small documentation and some kind of readme.

An empty project is exactly what it sounds like: It's got no files in it, which means it's up to you to create and add .cpp files, and write the necessary functions, etc.

when I did create it, it seems like the "precompiled header" check box is grey


Make sure that the project you're creating is a win32 console application, and not a win32 project. If it's still gray, let me know.

And when I do actually create a empty project, how do I write code?


What do you mean? You add a file to your project, and you write things in the file.

why does the code in the book doesn't have a main function?

Well, often times books will only feature code in sections where it's appropriate, which makes sense. In a chapter which teaches you about classes, the book might only highlight a class implementation, as opposed to writing out the entire source code. Even by studying a class implementation, you should get an idea of its intended use.
Oh...So you can't compile a class impementation? Thank you very much. And yes, the box isn't grey anymore. Thank you soooooo much. I always thought that when it said class impementation it was another way to write code. Thank you.
And also, I have the question, what's the use of a class impementation if you cannot run it? I didn't entirely understand the book, software engineering issues?
Oh...So you can't compile a class impementation

Uhm, that's not what I meant. Sorry if I wasn't clear.

I'm assuming it's this paragraph which threw you off:

In a chapter which teaches you about classes, the book might only highlight a class implementation, as opposed to writing out the entire source code. Even by studying a class implementation, you should get an idea of its intended use.


All I meant was that books will sometimes only show code that is relevant to what it's teaching. In the first chapter you might cover the main function. In a later chapter you might cover classes, in which case, the book will show no code for a main function, because you're learning about classes, even though a main function is required.

All I mean with class implementation is what a class does. When you declare functions in a class, you're telling the compiler that those functions have a body elsewhere. Therefore, the class functions are implemented elsewhere (like, a .cpp file).

Sometimes it's hard for me to simplify my explanations, so if there's any confusion please let me know.
Last edited on
Ok so you can compile a class impementation? So if you do need a main function, how exactly do I write one into the code? So you declare the functions into the header, and implement in the cpp? Is it like this?
GradeBook.h:
#include "stdafx.h"
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:

GradeBook( string ); // constructor that initializes courseName
void setCourseName( string ); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage();



private:
string courseName; // course name for this GradeBook
; };// end class GradeBook


ConsoleApplication9:


#include "stdafx.h"



#include <iostream>

#include "GradeBook.h"

using namespace std;

GradeBook::GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string GradeBook::getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()<< "!" << endl;
} // end function displayMessage

int main()
{
GradeBook mygradebook("Electronics");

mygradebook.displaymessage()<< endl;
system("pause");
}

Because I didn't really understand the book. Learning c++ by myself is some what confusing. Thank you so much, I appreciate it
Last edited on
If you format your code using code tags, it's a lot easier to read.

I would split up the code into three files. You can use this simple example program as sort of a template for your project:

radio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef _RADIO_H_
#define _RADIO_H_

//This file contains the class definition(s).

class Radio {
public :
	Radio();

	void toggle();//This function turns the radio object on if it's off, and off if it's on.
	bool on;

};

#endif 


radio.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "radio.h"

//This file contains the class implementations.

Radio::Radio() {
	on = false;
}

void Radio::toggle() {
	on = !on;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "radio.h"

//This file contains the main function (driver program).

int main() {
	Radio radio;//Radio object, which by default is turned off.

	std::cout << std::boolalpha << "The radio is on:\t" << radio.on << std::endl;

	radio.toggle();//This turns the radio on.

	std::cout << std::boolalpha << "The radio is on:\t" << radio.on << std::endl;
	
	radio.toggle();//This turns the radio off again.

	std::cout << std::boolalpha << "The radio is on:\t" << radio.on << std::endl;

	std::cin.get();
	return 0;
}
Last edited on
OMG thank you soooooooooooooooooooooooooo much it is working now!!!One last question though, what's the meaning of declaring the functions in class? Whether the perimeter or the return type, (except variables, which of course you could just add to the cpp), they are all described in the cpp file. Why?
Hmm. It seems as though you didn't learn classes thoroughly.
Maybe you don't understand why programmers split code into different files...
Here's a thread, to which I responded a while ago:

http://www.cplusplus.com/forum/beginner/116779/

You should check out my response on that thread. I tried going into detail about the purpose of splitting code etc.

But just to recap, C++ is an object oriented programming language. This means that we can encapsulate data into so called structures or classes, to be later used as object types. Often times, it makes sense to allow an object to modify its data through some procedure unique to the object, which is why objects can have functions/methods.

In a header file (.h), we tell the compiler that a certain object type can exist.
In the same file, we also tell the compiler what kinds of functions such an object should be able to execute, and what kinds of data such an object can contain.

In a .cpp file, we typically give the functions and data meaning, i.e. giving functions a body (implementation).
" This means that we can encapsulate data into so called structures or classes, to be later used as object types. Often times, it makes sense to allow an object to modify its data through some procedure unique to the object, which is why objects can have functions/methods."

This part is somewhat confusing? What exactly is an object? I never really understood what object orient is
Pages: 12