简体   繁体   中英

Build C++ files: .h and .cpp

We are trying to learn how to build a c++ file with .h and .cpp files. We keep getting the same error :error LNK2019: unresolved external symbol WinMain@16 referenced in function _ _tmainCRTStartup, and we don't know what we are doing wrong. Here is the code from our 3 files:


// First cpp file

#include "stdafx.h"
#include <iostream>
#include "chayaGradeBook.h.h"
namespace std;

//constructor
GradeBook::GradeBook (string name)
{
setCourseName(name);
}
void GradeBook::setCourseName(string name)
{
if (name.size() <= 25)
    courseName=name;
if (name.size() > 25)
{
    courseName = name.substr (0, 25);

    cerr << "Name \"" <<name<< "\"exceeds maximum length (25).\n"<< "Limiting courseName to first 25 charachters.\n"<<endl;
}
}

string GradeBook::getCourseName() const
{
return courseName;
}

void GradeBook::displayMessage() const
}  
cout << "Welcome to the grade book for \n" << getCourseName() << "!" <<endl;
}

// chayaGradeBook.h.h

#include <string>


//Grade Book class definition
class GradeBook
{
public:
explicit GradeBook (std::string);
void setCourseName (std::string);
std::string getCourseName() const;
void displayMessage() const;
private:
std::string courseName;
};

main.cpp file:

#include "stdafx.h"
#include <iostream>
#include "chayaGradeBook.h.h"
#include "chayaGradeBook.cpp"
using namespace std;


int main()
{
GradeBook gradeBook1 ("CS101 Introduction to Programming in C++");
GradeBook gradeBook2 ("CS102 Data Structures");

cout<< "gradeBook1's initial course name is: "
    << gradeBook1.getCourseName()
    <<"\ngradeBook2's inital course name is: "
    << gradeBook2.getCourseName()<< endl;

gradeBook1.setCourseName("CS101 C++ Programming");

cout <<"\ngradeBook1's course name is: "
    <<gradeBook1.getCourseName()
    << "\ngradeBook2's course name is: "
    << gradeBook2.getCOurseName() <<endl;
}

Linker can't find yours WinMain() which is an antry point for windows application. Create new console project and copy yours file into it this should help.

It is the peculiarity of Windows OS. Microsoft think that WinMain are better for "native Windows" applications (than main ) :-)
By the logic, "native Windows" applications are closely associated with Windows, based in the native Windows API and are not portable. It is likely a case of "freestanding environment" in terms of C++.
You need "console application project" in IDE...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM