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.
Related
This question already has answers here:
c++ class why need main?
(5 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I'm new to c++ and I just don't get this error on visual studio community. I already tried so many things like changing properties settings. Like configuration Type. Also the Subsystem from Console to Windows Rewriting it. At this point I just really need help.
So this is my Header file.
#include <iostream>
#include<string>
using namespace std;
#ifndef University_h
#define University_h
class University
{
public:
University();
void setUniversityID(int);
int getUniversityID();
void setCourseIdSectId(int, int);
string getCourseIdSectId();
void setCredits(int);
int getCredits();
void setDaysTime(int, string);
string getDaysTime();
void setRoomID(int, int);
string getRoomID();
void setMaxallotment(int);
int getMaxallotment();
void setCampus(string);
string getCampus();
void setCourseInstructor(string);
string getCourseInstructor();
void setEnrollStudents(int);
int getEnrollStudents();
void setCourseStatus(string);
string getStatus();
private:
int universityIDNumber;
int DeptID;
int sectID;
int noOfCredits;
int days;
string time;
int buildingID;
int roomID;
int maxEnrollment;
string courseCampus;
string courseInstructor;
int noOfStudentsEnrolled;
string courseStatus;
};
#endif
And this is my main.cpp
#include "University.h"
University::University()
{
universityIDNumber = 0;
DeptID = 0;
sectID = 0;
noOfCredits = 0;
days = 0;
time = "00:00";
buildingID = 0;;
roomID = 0;
maxEnrollment = 0;
courseCampus = "unknown";
}
void University::setUniversityID(int ID)
{
universityIDNumber = ID;
}
int University::getUniversityID()
{
return universityIDNumber;
}
void University::setCourseIdSectId(int deptId, int sId)
{
DeptID = deptId;
sectID = sId;
}
string University::getCourseIdSectId()
{
return DeptID + " " + sectID;
}
void University::setCredits(int credits)
{
noOfCredits = credits;
}
int University::getCredits()
{
return noOfCredits;
}
void University::setDaysTime(int ds, string tm)
{
days = ds;
time = tm;
}
string University::getDaysTime()
{
return days + " " + time;
}
void University::setRoomID(int buildId, int roomid)
{
buildingID = buildId;
roomID = roomid;
}
string University::getRoomID()
{
return buildingID + " " + roomID;
}
void University::setMaxallotment(int maxNums)
{
maxEnrollment = maxNums;
}
int University::getMaxallotment()
{
return maxEnrollment;
}
void University::setCampus(string campusName)
{
courseCampus = campusName;
}
string University::getCampus()
{
return courseCampus;
}
void University::setCourseInstructor(string name)
{
courseInstructor = name;
}
string University::getCourseInstructor()
{
return courseInstructor;
}
void University::setEnrollStudents(int enrollment)
{
noOfStudentsEnrolled = enrollment;
}
int University::getEnrollStudents()
{
return noOfStudentsEnrolled;
}
void University::setCourseStatus(string status)
{
courseStatus = status;
}
string University::getStatus()
{
return courseStatus;
}
Do you remember any lectures (or sections in your text-book) telling you that all C++ programs must have a main function?
Well for Windows GUI programs this is called WinMain instead.
Unless you want a Windows GUI program you should make sure that you create a console type project in Visual Studio, and use the existing templates so Visual Studio creates a main function for you.
I am a beginner and running into the above-said error. The following is the complete code from three files:
ball.h:
#ifndef BALL_H
#define BALL_H
namespace
{
inline constexpr double gravity{ 9.81 };
}
double getInitialHeight(void);
double calculateHeight(double, int);
void printHeight(double, int);
void calculateAndPrintHeight(double, int);
void solve(void);
#endif
ball.cpp:
#include "ball.h"
#include <iostream>
double getInitialHeight()
{
std::cout << "Enter the height of the tower in meters ";
double initialHeight{};
std::cin >> initialHeight;
return initialHeight;
}
double calculateHeight(double initialHeight, int secondsPassed)
{
double distanceFallen{ BALL_H::gravity * secondsPassed * secondsPassed / 2.0 };
double currentHeight{ initialHeight - distanceFallen };
return currentHeight;
}
void printHeight(double height, int secondsPassed)
{
if (height > 0.0)
{
std::cout << "At " << secondsPassed << " seconds, the ball is at height\t" << height << " meters.\n";
}
else
{
std::cout << "At " << secondsPassed << " seconds, the ball is on the ground.\n";
std::exit(0);
}
}
void calculateAndPrintHeight(double initialHeight, int secondsPassed)
{
double height{ calculateHeight(initialHeight, secondsPassed) };
printHeight(height, secondsPassed);
}
void solve()
{
const double initialHeight{ getInitialHeight() };
int secondsPassed{ 0 };
while (true)
{
calculateAndPrintHeight(initialHeight, secondsPassed);
secondsPassed++;
}
}
Solution.cpp(the main project file in the solution):
#include <iostream>
#include "ball.h"
int main()
{
solve();
return 0;
}
I understand that this problem is caused because linker cannot find a reference to solve(). However, I am not sure how to solve the issue. One easy solution is to simply include ball.cpp rather than ball.h:
#include <iostream>
#include "ball.cpp"
int main()
{
solve();
return 0;
}
This code works but I'd like to know how to use headers instead since I am not sure if this a good practice.
EDIT:
Here's the error list:
When you add header file, you could right click Header Files and select Add->New Item.
When you add .cpp file, you could right click Source Files and select Add->New Item.
Then, add the following code in Source.cpp.
#include <iostream>
#include "ball.h"
int main()
{
solve();
return 0;
}
Finally, it works fine.
This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 5 years ago.
Can't understand what doesn't this freak(the Linker).
I tried to find answer in google but usually people do connect several times the same file.
I have the same Player.cpp connects once in the main file, and inside Player.cpp connected Player.h, that's all. What's wrong?
1>------Rebuild All started : Project: ConsoleApplication1, Configuration : Debug Win32------
1>stdafx.cpp
1>Player.cpp
1>ConsoleApplication1.cpp
1>Generating Code...
1>Player.obj : error LNK2005 : "public: __thiscall Player::Player(int,int)" (? ? 0Player##QAE#HH#Z) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: __thiscall Player::~Player(void)" (? ? 1Player##QAE#XZ) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: int __thiscall Player::getX(void)" (? getX#Player##QAEHXZ) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: int __thiscall Player::getY(void)" (? getY#Player##QAEHXZ) already defined in ConsoleApplication1.obj
1>C:\Users\New\Documents\Visual Studio 2017\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1169 : one or more multiply defined symbols found
ConsoleApplication1.cpp:
#include "stdafx.h"
#include "windows.h"
#include <conio.h>
#include <iostream>
#include "main.h"
using namespace std;
void Render(CELL** vector);
void WaitForAction();
void Move(ACTION a);
int main()
{
CELL** vector = new CELL*[SIZE_MAP_Y];
for (int i = 0; i < SIZE_MAP_Y; i++) {
vector[i] = new CELL[SIZE_MAP_X];
for (int j = 0; j < SIZE_MAP_X; j++) {
vector[i][j] = CELL::EMPTY;
}
}
Player* player = new Player(2, 3);
cout << player->getX() << ", ";
cout << player->getY() << endl;
system("pause");
while (true) {
system("cls");
Render(vector);
WaitForAction();
}
for (int i = 0; i < 10; i++)
delete[] vector[i];
delete[] vector;
system("pause");
return 0;
}
void Render(CELL** vector) {
for (int y = 0; y < SIZE_MAP_Y; y++) {
for (int x = 0; x < SIZE_MAP_X; x++) {
if (vector[y][x] == CELL::EMPTY) cout << "#";
else if (vector[y][x] == CELL::PLAYER) cout << "O";
else cout << "?";
}
cout << endl;
}
}
void WaitForAction() {
char ch;
ch = _getch();
if (ch == 'ф' || ch == 'a') Move(ACTION::LEFT);
else if (ch == 'ы' || ch == 's') Move(ACTION::DOWN);
else if (ch == 'в' || ch == 'd') Move(ACTION::RIGHT);
else if (ch == 'ц' || ch == 'w') Move(ACTION::UP);
}
void Move(ACTION a) {
if (a == ACTION::LEFT) {
}
}
main.h:
#include "Player.cpp"
#define SIZE_MAP 10
#ifdef SIZE_MAP_Y
#undef SIZE_MAP_Y
#endif
#ifdef SIZE_MAP_X
#undef SIZE_MAP_X
#endif
#define SIZE_MAP_Y SIZE_MAP
#define SIZE_MAP_X (SIZE_MAP_Y * 2)
enum CELL { EMPTY, PLAYER };
enum ACTION { LEFT, RIGHT, DOWN, UP };
Player.cpp:
#include "stdafx.h"
#include "Player.h"
Player::Player(int x, int y)
{
this->x = x;
this->y = y;
}
Player::~Player()
{
}
int Player::getX() {
return this->x;
}
int Player::getY() {
return this->y;
}
Player.h:
#pragma once
class Player
{
public:
Player(int x, int y);
~Player();
int getX();
int getY();
private:
int x;
int y;
};
Include the Player.h header in your ConsoleApplication1.cpp source file:
#include "Player.h"
and remove the Player.cpp source file from your main.h header which is the cause of multiple definitions:
#include "Player.cpp" // <- remove this line
Declarations should go into header files, definitions should go inside source files. Source files (*.cpp) should include header files (*.h), not the other way around.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm a visual studio 2015 c++ newby who's trying to write some game code at home.
I'm getting this link error:
LNK2019 unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall display_utils::fit_int_2(int)" (?fit_int_2#display_utils##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "public: void __thiscall bat_stats::disp_bat_stats(struct bat_stats::bat_stats_typ)" (?disp_bat_stats#bat_stats##QAEXUbat_stats_typ#1##Z)
It apparently doesn't like the string I'm using to access the returned string from function fit_int_2. I've google searched for a solution, but can't find anything that fixes my problem. Note that the code compiled and linked before i I added the fit_int_2 call. Thanks in advance if you can help me out. The code is below:
bat_stats.h
#pragma once
class bat_stats
{
public:
struct bat_stats_typ
{
int gm;
int ab;
int ht;
int dbl;
int trpl;
int hr;
int rbi;
int sb;
int cs;
int bb;
int ibb;
int sf;
int sac;
int k;
int gidp;
int err;
float ave;
float slg;
float obp;
};
void disp_bat_hdr();
void disp_bat_stats( bat_stats_typ );
private:
int dummy;
};
bat_stats.cpp
#include <iostream>
using std::cout;
std::cin;
#include <string>
using std::string;
#include "bat_stats.h"
#include "display_utils.h"
void bat_stats::disp_bat_hdr()
{
cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n";
}
void bat_stats::disp_bat_stats( bat_stats_typ bat )
{
display_utils dut;
string s;
s = dut.fit_int_2( bat.gm ); // <- the problem is here!
cout << s << bat.gm << " ";
cout << bat.ab << "\n\n";
}
display_utils.h
#pragma once
#include <string>
using std::string;
class display_utils
{
public:
void insert_5_lines();
string fit_int_2( int );
private:
int dummy;
};
display_utils.cpp
#include <iostream>
using std::cout;
#include "display_utils.h"
void display_utils::insert_5_lines()
{
cout << "\n\n\n\n\n";
}
string fit_int_2(int i0)
{
string s0 = "";
if (i0 < 10)
{
s0 = " ";
}
return s0;
}
You need to change
string fit_int_2(int i0)
to
string display_utils::fit_int_2(int i0)
(You need to define the member function - currently you're defining an unrelated global function.)
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I created Linked list and it has no problem and runs. Plus i have to include Binary Tree. and its giving me this error
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall SDI::BinaryTree::insert(int)" (?insert#BinaryTree#SDI##QAEHH#Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall SDI::BinaryTree::preOrder(void)" (?preOrder#BinaryTree#SDI##QAEHXZ) referenced in function _main
1>C:\Users\Muugii\Documents\Visual Studio 2013\LinkedList\Debug\LinkedList.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
BinaryTree.h
ifndef SDI_BINARYTREE
define SDI_BINARYTREE
include
namespace SDI
{
class BinaryTree
{
class BTNode
{
public:
int data;
BTNode *left;
BTNode *right;
};
public:
BinaryTree();
~BinaryTree();
int insert(const int value);
int preOrder() const;
int clearTree();
private:
BTNode *headPtr;
int insert(const int value, BTNode *leaf);
int clearTree(BTNode *leaf) const;
int preOrder(BTNode *leaf) const;
};
};
#endif
BinaryTree.cpp
#include"BinaryTree.h"
#include <iostream>
using namespace std;
SDI::BinaryTree::BinaryTree()
{
headPtr = nullptr;
}
SDI::BinaryTree::~BinaryTree()
{
//clearTree(headPtr);
}
int SDI::BinaryTree::insert(const int value, BTNode *leaf)
{
if (value < leaf->data)
{
if (leaf->left != nullptr)
{
insert(value, leaf->left);
}
else
{
leaf->left = new BTNode;
leaf->left->data = value;
leaf->left->left = nullptr;
leaf->left->right = nullptr;
}
}
else if (value >= leaf->data)
{
if (leaf->right != nullptr)
{
insert(value, leaf->right);
}
else
{
leaf->right = new BTNode;
leaf->right->data = value;
leaf->right->left = nullptr;
leaf->right->right = nullptr;
}
}
return 1;
}
Main.cpp
#include <string>
#include <iostream>
#include "Linkedlist.h"
#include "BinaryTree.h"
using namespace std;
int main( int argc, const char *argv[])
{
SDI::LinkedList myList;
//SDI::BinaryTree myTree;
int inp;
....
if (inp2 == 'a')
{
int num;
cout << "\nPlease enter a value to add: ";
cin >> num;
myTree.insert(num);
cout << "\n\t\tValue has been successfully saved\n\n\n";
}
I have tried looking up online, couldn't find any. It would be great help.
As I see you have two functions with name insert. And the first one
int insert(const int value);
was only declared but not defined.
Which part of the error message is unclear to you? It clearly states that you haven't implemented two of your methods:
int SDI::BinaryTree::insert(const int value) {
return 0; // the implementation probably needs some patching up
}
int SDI::BinaryTree::preOrder() const {
return 0; // as does this one
}