LNK2019 error for Visual Studio - unresolved external symbol - c++

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.

Related

Undefined symbols for architecture x86_64: (linker issue?)

I'm doing the following problem :
Add on to your Rectangle class from last time:
1)Create a new class TestRect which is a friend class of Rectangle. Inside this class, implement a member function called TestRect::tester, which takes a rectangle as a parameter and tests that both length and width are > 0.
2)Create a main() function which makes three rectangles r1, r2, and r3, each of which has length and width 20. Then call your TestRect::tester function on each of these rectangles and print out the result
I think I did part 1) correctly but part 2) does not give me the output I'm looking for. I do not know how to fix my code from the following output I got :
cppcompile rectEnhanceStaticFriends
Undefined symbols for architecture x86_64:
"TestRect::tester(Rectangle&)", referenced from:
_main in rectEnhanceStaticFriends.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
cppcompile:15: no such file or directory: ./rectEnhanceStaticFriends
Can you help me how to fix it? Seems a linker issue, that I did not find how to fix it. Thank you for reading through. (Ps: I compile with VSC)
Here is my code :
rectEnhanceStaticFriends.cpp
#include <iomanip>
#include <cstring>
#include <string>
#include "rect.h"
using namespace std;
int main()
{
Rectangle::setYards(100);
Rectangle r1(20, 20, "Kitchen");
Rectangle r2(20, 20, "Bathroom");
Rectangle r3(20, 20, "Office");
TestRect tr;
cout << "Test on r1: " << tr.tester(r1) << endl;
cout << "Test on r2: " << tr.tester(r2) << endl;
cout << "Test on r3: " << tr.tester(r3) << endl;
Rectangle house[] = {Rectangle(10, 12, "Kitchen"),
Rectangle(20, 20, "Bedroom"),
Rectangle(8, 12, "Offce")};
for (int i = 0; i < 3; i++)
{
if (strcmp(house[i].printName(), "Offce") == 0)
{
// cout << "oui\n";
house[i].setName("Office");
};
cout << "Area for " << house[i].printName() << " is : " << house[i].getArea() << endl;
}
if (house[1].getArea() > house[2].getArea() && house[1].getArea() > house[3].getArea())
{
cout << house[1].printName() << " has the biggest area.\n";
}
else if (house[2].getArea() > house[1].getArea() && house[2].getArea() > house[3].getArea())
{
cout << house[2].printName() << " has the biggest area\n";
}
else
{
cout << house[3].printName() << " has the biggest area\n";
}
//there is an error house[3] go beyond the array..
return 0;
}
testRect.h
#ifndef TESTRECT_H
#define TESTRECT_H
class Rectangle; //forward declaration of class Rectangle
class TestRect
{
public:
bool tester(Rectangle &);
};
#endif
testRect.cpp
#include <iostream> //
#include <iomanip>
#include <string>
#include "testRect.h"
#include "rect.h"
bool TestRect::tester(Rectangle &r)
{
bool testResult = false;
if (r.width > 0 && r.length > 0)
testResult = true;
return testResult;
}
rect.h
// Rec header file
#ifndef RECT_H
#define RECT_H
#include "testRect.h"
class Rectangle
{
private:
double width;
double length;
char *name;
static double yardsAvail; //indicate how many yards of perimeter are available to make rectangle
void initName(const char *n);
void initName(const char *n, int size);
public:
//constructors
Rectangle();
Rectangle(double, double,
const char *);
//destructor
~Rectangle() { delete[] name; };
void setWidth(double);
void setLength(double);
void setWidth(char *);
void setLength(char *);
void setName(const char *);
int getWidth() const;
int getLength() const;
double getArea() const;
char *printName() const
{
return name;
}
//added parts
static void setYards(double);
friend class TestRect;
friend bool TestRect::tester(Rectangle &);
};
double Rectangle::yardsAvail = 0; //added parts
#endif
rect.cpp
#include <iostream> //
#include <iomanip>
#include <string>
#include "rect.h"
#include "testRect.h"
using namespace std;
Rectangle::Rectangle()
{
width = 0;
length = 0;
initName("Default");
}
Rectangle::Rectangle(double x, double y, const char *z)
{
width = x;
length = y;
initName(z);
double yardsReqd = 2 * x + 2 * y;
if (yardsAvail - yardsReqd < 0)
{
cout << "Not enough yard..\n";
width = 0;
length = 0;
}
yardsAvail -= yardsReqd;
}
void Rectangle::initName(const char *n)
{
name = new char[258];
strcpy(name, n);
};
void Rectangle::initName(const char *n, int size)
{
name = new char[size];
strcpy(name, n);
};
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
void Rectangle::setName(const char *newname)
{
//newname.newName = "Office";
strcpy(name, newname);
}
double Rectangle::getArea() const
{
return width * length;
}
//added part
void Rectangle::setYards(double y)
{
yardsAvail = y;
}

C++: undefined reference to Constructor

I just started working with C++ and am working on an exercise that deals with polymorphic pointers. I'm having trouble trying to solve an error message I believe I'm getting from my Rectangle.cpp as I call the class from my main.cpp.
The error message:
undefined reference to 'Rectangle::Rectangle(double, double)'
Main.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
//////////////////////////////////////////////
// --- FUNCTIONS DECLARATION---
void introduceShape(Shape*);
double calculateShapeArea(Shape*);
double calculateShapeCircumferece(Shape*);
int main()
{
Rectangle rectangle1(5,2);
// Rectangle *rec1 = new Rectangle(5,2);
introduceShape(&rectangle1);
cout << "My area is: " << calculateShapeArea(&rectangle1) << ", my circumference is: " << calculateShapeCircumferece(&rectangle1) << endl << endl;
return 0;
}
//////////////////////////////////////////////
// --- FUNCTIONS ---
void introduceShape(Shape* shapeToIntroduce)
{
return shapeToIntroduce->introduce();
}
double calculateShapeArea(Shape* shapeToCalculate)
{
return shapeToCalculate->calculateArea();
}
double calculateShapeCircumferece(Shape* shapeToCalculate)
{
return shapeToCalculate->calculateCircumference();
}
Rectangle.h
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"
#include <iostream>
using namespace std;
class Rectangle: public Shape
{
double width;
double height;
public:
Rectangle(double , double );
void introduce();
double calculateArea();
double calculateCircumference();
};
#endif // RECTANGLE_H_INCLUDED
Rectangle.cpp
#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(double width, double height)
{
this->width = width;
this->height = height;
}
void Rectangle::introduce()
{
cout << "I AM A RECTANGLE !" << endl;
}
double Rectangle::calculateArea()
{
return width*height;
}
double Rectangle::calculateCircumference()
{
return 2*(width+height);
}
Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
class Shape
{
public:
virtual void introduce() = 0;
virtual double calculateArea() = 0;
virtual double calculateCircumference() = 0;
};
#endif // SHAPE_H_INCLUDED
The error is generated by the linker because it can not see where the definition of the constructor is located.
If you are using an IDE, you should add .cpp file to the project so that it can be compiled and the definition would be found by the linker. It not, then you have to compile it yourself -assuming you are using gcc:
g++ Rectangle.cpp
will combine cpp files into one executable and should not show you that error.
Visit this post

multiple inheritance variables returning correctly

I'm working on a multiple inheritance exercise and I'm running into a strange error. My code is not returning the correct information to the console. It is just outputting zero, I have checked multiple times in my code and I can't seem to find anything obviously wrong. I'm fairly new to C++ so any help I would appreciate it, in addition to any other critique.
The console will output
Maverick
South station
50 - passengers, is ok
40 - speed
0 - it is supposed to take distance/mph and output 2.6 in this case, but is returning nothing.
MBTA.cpp
#include "MBTA.h"
//objects
transportation Dest;
MBTA::MBTA()
{
}
MBTA::MBTA(string strIn, string strInTransport, int iIn, int distIn, int eIn)
{
setTrain(strIn);
//Destination
Dest.setTransport(strInTransport);
//set passengers
setPass(iIn);
Dest.setMilesToDest(distIn);
engine.setMPH(eIn);
//outputs train information
printTrainDestinationHours();
//used printf as I was running into issue with using cout here
//cout << " I am going to ";
printf("I am going to %s\n", Dest.getTransport().c_str());
//uses engine stats function
cout << "I go " << engine.getMPH() << endl;
printf("It will take me %.2f hours to arrive", redline.getTotal());
}
void MBTA::setTravelDist(int iIn)
{
double destdistance = Dest.getDist();
double trainMPH = engine.getMPH();
//this divides miles by MPH, this might return a float
redline.setTotal(50, 10);
}
MBTA::~MBTA()
{
}
MBTA.H
#pragma once
#include "train.h"
class MBTA :
public train
{
public:
engine engine;
train redline;
MBTA();
//train, destination, passengers, traveldist, speed
MBTA(string, string, int, int, int);
void setTravelDist(int);
//double getTotal();
//uses engine stats function
//double total = 0;
~MBTA();
};
Train.cpp
#include "train.h"
#include <iostream>
#include <cstdio>
#include <cmath>
using std::string;
using std::cout;
using std::endl;
//object member for transport
transportation tTrain;
train::train()
{
}
void train::printTrainDestinationHours()
{
printf("\n\nTrain type: %s\n", getTrain().c_str());
//passengers
printf("I have %d passengers\n", getPass());
}
void train::setPass(int iIn)
{
passengers = iIn;
}
int train::getPass()
{
return passengers;
}
void train::setTrain(string strIn)
{
trainName = strIn;
}
string train::getTrain()
{
return trainName;
}
void train::setTotal(int aIn, int bIn)
{
//dist / mph
total = aIn / bIn;
}
double train::getTotal()
{
return total;
}
train::~train()
{
}
Train Header
#pragma once
#include <iostream>
#include <cstdio>
using std::string;
using std::cout;
using std::endl;
#include "engine.h"
#include "transportation.h"
class train : public transportation
{
public:
train();
void printTrainDestinationHours();
//set and get destination
//num of pass
void setPass(int);
int getPass();
//train
void setTrain(string);
string getTrain();
//distance
void setTotal(int, int);
double getTotal();
~train();
private:
engine engineStats;
int total = 0;
string trainName = "";
string destination = "";
int passengers = 0;
};
engine.cpp
#include "engine.h"
engine::engine()
{
}
void engine::setMPH(int iIn)
{
MPH = iIn;
}
int engine::getMPH()
{
return MPH;
}
engine::~engine()
{
}
engine header
#pragma once
class engine
{
public:
engine();
//return
void setMPH(int);
int getMPH();
~engine();
protected:
int MPH = 0;
};
'''
transportation cpp
'''
#pragma once
class engine
{
public:
engine();
//return
void setMPH(int);
int getMPH();
~engine();
protected:
int MPH = 0;
};
transportation header
#include "transportation.h"
transportation::transportation()
{
}
void transportation::setTransport(string strIn)
{
destination = strIn;
}
string transportation::getTransport()
{
return destination;
}
void transportation::setMilesToDest(int iIn)
{
MilesToDestination = iIn;
}
int transportation::getDist()
{
return MilesToDestination;
}
transportation::~transportation()
{
}
main file
#include <iostream>
using std::string;
using std::cout;
using std::endl;
using std::cin; //for ignore
#include "challenger.h"
#include "MBTA.h"
#include "plane.h"
int main()
{
//object composition of vehicle type
// vehicle type location, passengers, MPH , distance
challenger SRT8707("Boston", 2, 100, 200);
plane boeing("boeing", "houston", 50, 500, 300);
MBTA redline("Maverick", "South station", 50, 100, 40);
//pause and blank line
cout << endl << endl;
cin.ignore();
}

C++ inheritance how to get variables values

I'm trying to create a small program for a lessons.
But In My class that herit , I don't have values of the superclass.
, here's the code
Point.h
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int a, int b);
void set_values (int a, int b, int c);
void affichervaleurs();
protected:
int x ;
int y ;
int z ;
};
#endif
Point.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(){
x=0;
y=0;
z=0;
};
void Point::set_values (int a, int b){
x=a;
y=b;
}
void Point::set_values (int a = 0, int b = 0, int c = 0){
x=a;
y=b;
z=c;
}
void Point::affichervaleurs(){
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
}
Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre:public Point{
public:
int Aire (){
};
void affichercar(){
};
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
#include "Point.h"
class Carre:public Point{
public:
//Carre::Carre(int a, int b);
int Aire (){
return (x * y);
}
void affichercar(){
cout << "Coordonnees X:" << x << endl;
}
};
main.cpp
#include <cstdlib>
#include <iostream>
#include "Carre.h"
#include "Point.h"
using namespace std;
int main()
{
Point MonPoint ;
cout << "Default values:" << endl;
MonPoint.affichervaleurs();
MonPoint.set_values(4,6);
cout << "Setting values:" << endl;
MonPoint.affichervaleurs();
Carre MonCarre;
MonCarre.set_values(4,6,0);
MonCarre.set_values(5,8);
MonCarre.affichercar();
cout << MonCarre.Aire() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
The problem is, when I call this line :
cout << MonCarre.Aire() << endl;
it returns a strange number. I'm sure that the problem is here :
int Aire (){
return (x * y);
};
Like the x and y are not linked with the superclass.
Can someone help me how to access my x and y values from my superclass in my class Carre ?
Other strange thing this line doesn't appear :
MonCarre.affichercar();
Thanks
You define the functions Aire and affichercar in Carre.h, but you need to declare them only if you want to write an implementation in Carre.cpp.
Edit: and your Carre.cpp is also wrong. You just need to rewrite Carre.* files.
Edit2. Let me do some code review and give you a working version of your code - just summarizing what was discussed in comments and my own thoughts.
Point.h
void set_values (int a, int b);
void set_values (int a, int b, int c);
If you want to define the default values of the arguments of the second function, you should do it in its declaration as was suggested in comments to your question. So it will be
void set_values (int a, int b);
void set_values (int a = 0, int b = 0, int c = 0);
But in this case it's not clear what function should be called if you write, for example, set_values(1, 2), therefore your code won't compile because of ambiguity (as also was mentioned in the comments). So you just have to keep only one extended version of this function with the default arguments. The Point.h will be in this case:
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int a = 0, int b = 0, int c = 0);
void affichervaleurs();
protected:
int x ;
int y ;
int z ;
};
#endif
The Point.cpp is also changed a little bit:
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point()
// it's better to initialize these variables here
// : x(0), y(0), z(0)
{
x=0;
y=0;
z=0;
}
void Point::set_values (int a, int b, int c){
x=a;
y=b;
z=c;
}
void Point::affichervaleurs(){
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
}
Now let's consider the Carre.h file
class Carre:public Point{
public:
// IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
int Aire (){};
// IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
void affichercar(){};
};
Now I'm starting answering your questions:
The problem is, when I call this line :
cout << MonCarre.Aire() << endl;
it returns a strange number.
Yes, it's a strange number returned by the function defined in the Carre.h (not Carre.cpp) file.
I'm sure that the problem is here :
int Aire (){
return (x * y);
};
No, as you see, it's not.
Like the x and y are not linked with the superclass.
In fact they are.
Can someone help me how to access my x and y values from my superclass in my class Carre ?
You already have this access. It's okay to use protected members from base class in derived class in case of public inheritance.
Other strange thing this line doesn't appear : MonCarre.affichercar();
You mean it does nothing? But it's in perfect consistency with how you implement (define) the function in Carre.h (not Carre.cpp) file.
So let me change the code to make it work.
Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre:public Point{
public:
int Aire (); // now it's declaration
void affichercar(); // now it's declaration
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
int Carre::Aire (){
return (x * y);
}
void Carre::affichercar(){
cout << "Coordonnees X:" << x << endl;
}
I didn't compile this for myself, but the whole idea should be clear.
access my x and y values
From within Carre, you simply access them by 'x' and 'y'.
From outside, you cannot as they are not public.

Microsoft C++ exception: std::bad_alloc. What could be causing it?

Same code that I was working on last night, has thrown up a whole new error. One that I've never encountered before, and I am at the point of considering throwing things at my PC. But, everyone here was very helpful last night, so I thought I'd see if anyone had any ideas on this new problem.
Something is causing "Microsoft C++ exception: std::bad_alloc" and I think it's within the first line of the main.cpp, but as that is just creating the player as a Hero (child of creature) class. I can't see why it can't do it.
I know it's probably something stupid, that I've done badly ... but any help would be appreciated!
//main.cpp
#include "Creature.h"
#include "Hero.h"
#include "Monster.h"
#include <conio.h>
using namespace std;
int main()
{
Hero player(1);
Monster baddie;
player.setX(1);
player.setY(1);
baddie.setX(20);
baddie.setY(20);
player.Display();
baddie.Display();
baddie.chase(player);
player.Display();
baddie.Display();
_getch();
return 0;
}
===================================
//Creature.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Creature
{
protected:
int m_xpos;
int m_ypos;
string m_name;
public:
Creature(string name, int xpos, int ypos);
void Display(void);
void left(void);
void right(void);
void up (void);
void down(void);
void setX(int x);
void setY(int y);
int getX(void);
int getY(void);
};
===================================
//monster.h
#pragma once
#include "Creature.h"
class Monster : public Creature
{
public:
Monster();
void chase(class Hero);
bool eaten(class Hero);
};
===================================
//Hero.h
#pragma once
#include "Creature.h"
class Hero : public Creature
{
private:
int m_lives;
int m_score;
public:
Hero(int lives);
void Display(void);
void setScore(void);
};
===================================
//creature.cpp
#include "Creature.h"
Creature::Creature(string name, int xpos, int ypos)
{
m_xpos = xpos;
m_ypos = ypos;
m_name = name;
}
void Creature::Display(void)
{
cout << m_name << endl;
cout << m_xpos << endl;
cout << m_ypos << endl;
}
void Creature::left(void)
{
m_xpos = m_xpos+1;
}
void Creature::right(void)
{
m_xpos = m_xpos-1;
}
void Creature::up(void)
{
m_ypos = m_ypos-1;
}
void Creature::down(void)
{
m_ypos = m_ypos+1;
}
void Creature::setX(int x)
{
m_xpos = x;
}
void Creature::setY(int y)
{
m_ypos = y;
}
int Creature::getX(void)
{
return m_xpos;
}
int Creature::getY(void)
{
return m_ypos;
}
===================================
//Hero.cpp
#include "Creature.h"
#include "Hero.h"
Hero::Hero(int lives) : Creature(m_name, m_xpos, m_ypos)
{
m_lives = lives;
}
void Hero::Display(void)
{
Creature::Display();
cout << "Lives: " << m_lives << endl;
cout << "Score: " << m_score << endl;
}
void Hero::setScore(void)
{
m_score = 0;
}
===================================
//Monster.cpp
#include "Creature.h"
#include "Monster.h"
#include "Hero.h"
Monster::Monster() : Creature(m_name, m_xpos, m_ypos)
{
}
void Monster::chase(Hero hero)
{
if(getX() < hero.getX())
{
right();
}
if(getX() > hero.getX())
{
left();
}
if(getX() < hero.getX())
{
down();
}
if(getX() >hero.getX())
{
up();
}
}
bool Monster::eaten(Hero hero)
{
if((getX() == hero.getX())&&(getX() == hero.getX()))
{
return true;
}
}
===================================
The problem lay in Hero::Hero(int lives) : Creature(m_name, m_xpos, m_ypos) and the equivilant with the Monster.cpp file.
Changing them to Hero::Hero(int lives) : Creature("", 0,0) fixed the memory problem.
Thanks again to a wonderful community!
Hopefully, you'll never see this code again! (fingers crossed!)
The error is with this line:
Hero::Hero(int lives) : Creature(m_name, m_xpos, m_ypos)
You cannot create the Creature sub-object by passing its own uninitialized data members to it. You need to pass some sort of valid values to the base-class constructor, like Creature("", 0, 0) for example.
The error is caused, somehow, by the attempt to copy an uninitialized std::string object.