error LNK2001: unresolved external symbol _mainCRTStartup?

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". What did I do wrong and how do I fix it? Thx
You've tried to create an executable, without a main() function. Every C++ program needs a main(). main() is the entry point to your program, the OS runs your program, by calling main().
You can compile your program without linking, i.e without trying to create a final executable, if you just want to check that your code will compile.
I'm not familiar with VisualStudio these days, but you should be able to right click on a file or find an option in the menu that's just "compile" rather than "build", but I'm just guessing wildly.
If you really must use the full build commands, then add a main() function, even if it's just a stub.

Also, I suggest not to do "using namespace std;" in your header file, but instead fully qualify every use of string like so std::string. When you get to the cpp file, you already have "using namespace std;" so you can drop the std:: prefix.

You have a stray semi-colon at the end of your class definition:
 
; };// end class GradeBook 


And finally, remember to use code tags when posting, to make your code easier to read.
Ok so now this is my code:
cpp:
#include "stdafx.h"

#include <iostream>

#include "GradeBook.h"

using namespace std;

int main()
{
system("pause");
}

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



heading:
#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


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
Your program doesn't actually do anything yet.

You've defined your class, but you haven't created any objects or told them to do anything yet.

The "press any key to continue" is the result of calling system("pause"), it's just like typing "pause" at the command line.

A picture (or code fragment) is worth a thousand words, so here's an example main(), using your code. It creates some objects of type GradeBook, and calls some of the member functions you've defined.

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
int main()
{
	// create obect of type GradeBook. The object is named mathsGrades
	// Based on your class definition,a GradeBook is created by using a string object
	// In this case, a string object is created for you using the string literal "Maths 101"
	// this string is then used to create the GradeBook object grades
	GradeBook mathsGrades("Maths 101"); 

	// create object of type string, called gradeBookName
	string gradeBookName("Physics 101"); 
	
	// create GradeBook called physicsGrades, using the string object gradeBookName
	GradeBook physicsGrades(gradeBookName); 
	
	// Call member function for object mathsGrades
	// In other words, tell mathsGrades to do something (displayMessage())
	mathsGrades.displayMessage();
	
	// Tell physicsGrades to displayMessage()
	physicsGrades.displayMessage();
	
	// Create another GradeBook, this one is called randomGrades
	GradeBook randomGrades("Radical Grade Book");
	
	// Tell radicalGrades to getCourseName()
	// Use the result of getCourseName(), by sending it to cout
	cout << "My last grade book is called " << randomGrades.getCourseName() << endl;
	
	// Tell randomGrades to do some other things
	randomGrades.setCourseName("Unknown Subject");
	randomGrades.displayMessage();
	
	system("pause");
}
Topic archived. No new replies allowed.