简体   繁体   中英

C++ What's wrong with this class?

It doesn't compile in Visual Studio, it says

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>position.exe : fatal error LNK1120: 1 unresolved externals

#include <iostream>
#include <cstdlib>

using namespace std;

class Position
{
private:
    int row;
    int column;
public:
    Position();         //constructor
    ~Position();        //destructor
    void setPos(int, int);  //set the position
    int getRow();       //return the current row
    int getColumn();    //return the current column
    void getPos();      //print the pos
    bool compare(int, int); //compare a row and column with the one in the class
};

Position::Position()
{}
Position::~Position()
{}
void Position::setPos(int x, int y)
{
    row = x;
    column = y;
}
int Position::getRow()
{
    return row;
}
int Position::getColumn()
{
    return column;
}
void Position::getPos()
{
    cout << "Row: " << row << "Column: " << column;
}
bool Position::compare(int x, int y)
{
    if(x == row && y == column)
        return true;
    else
        return false;
}

int main()
{
    return 0;
}

It compiled for me under Visual Studio Professional 2008.

Try creating a new project.

Select File->New and specify Project Type of Visual C++ -> Win32 -> Win32 Console Application.

Then click Ok.

Then click on Application Settings and uncheck "Precompiled header".

Then paste in your code and compile it successfully.

Make sure you're creating an empty project or a Win32 console application. If you make a Win32 Windows application then you will get this error.

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