0

I'm still decently new to learning C++ as I am just going through it in school now.

I'm having some issues getting my two .cpp files to compile as I'm getting "undefined reference to". I've done a ton of digging on the internet to solve some of these issues, such as making some minor editing to the tasks.json and even trying to compile through command line in the terminal window but nothing seems to be working. Yes; all the files are in the same project folder too. It's been causing me a serious headache.

Here is my code:

Asi2.cpp

    #include <iostream>
    #include "Point.h"
    using namespace std;

    int main()
    {
     Point point1(3.0, 6.0);
     Point point2(-4.5, 2.2);

     cout << "Distance between points: " << point1.calculateDistance(point2) << "\n";

     return 0;
    }

Point.cpp

#include <iostream>
#include <math.h>
#include "Point.h"
using namespace std;



Point :: Point()
{
 x = 0.0;
 y = 0.0;          
}

Point :: Point(float x1, float y1)
{
 x = x1;
 y = y1;
}

Point :: Point(Point &point1)
{
 x = point1.getX();
 y = point1.getY();   
}

Point :: ~Point()
{
}

void Point :: move(float x1, float y1)
{
 x = x1;
 y = y1;  
}

float Point :: calculateDistance(Point point1)
{ 
 double distance;
 double cal = ((point1.getX() - x) * (point1.getX() - x)) + ((point1.getY() - y) * (point1.getY() - y));
 distance = sqrt(cal);

 return distance;
}

void Point :: translate (float dx, float dy)
{
 x += dx;
 y += dy;  
}

void Point :: position (Point point1)
{
 if(x == point1.getX() && y == point1.getY())
   {
    cout << "These points are in the SAME spot!\n"; 
   }

 if(x == point1.getX() && y > point1.getY())
   {
    cout << "The selected point is BELOW the current point.\n"; 
   }

 if(x == point1.getX() && y < point1.getY())
   {
    cout << "The selected point is ABOVE the current point.\n"; 
   }     

 if(x > point1.getX() && y == point1.getY())
   {
    cout << "The selected point is to the LEFT of the current point.\n"; 
   }

 if(x < point1.getX() && y == point1.getY())
   {
    cout << "The selected point is to the RIGHT of the current point\n."; 
   } 

 if(x < point1.getX() && y < point1.getY())
   {
    cout << "The selected point is to the RIGHT and ABOVE the current point\n."; 
   }  

 if(x > point1.getX() && y > point1.getY())
   {
    cout << "The selected point is to the LEFT and BELOW the current point\n."; 
   }

 if(x < point1.getX() && y > point1.getY())
   {
    cout << "The selected point is to the RIGHT and BELOW the current point\n."; 
   }

 if(x > point1.getX() && y < point1.getY())
   {
    cout << "The selected point is to the LEFT and ABOVE the current point\n."; 
   }                           
}

float Point :: getX(void)
{
 return x;   
}

float Point :: getY(void)
{
 return y;   
}

void Point :: setX(float x1)
{
 x = x1;  
}

void Point :: setY(float y1)
{
 x = y1;  
}

float Point :: print(void)
{
 return x, y;  
}

Point.h

#ifndef POINT_H
#define POINT_H

class Point
{
 private:
   float x;
   float y;

 public:   
   Point();
   Point(float x1, float y1);
   Point(Point &point1);
   ~Point();
   void move(float x1, float y1); 
   float calculateDistance(Point point1);   
   void translate(float dx, float dy);
   void position(Point point1);
   float getX(void); 
   float getY(void);
   void setX(float x1);
   void setY(float y1);
   float print(void);
 };

 #endif

The Output I get when I try to Compile and run my Driver File (Asi2.cpp):

PS C:\Users\Luke\Documents\VSC\Asi 2> cd "c:\Users\Luke\Documents\VSC\Asi 2" ; if ($?) { g++ Asi2.cpp -o Asi2 } ; if ($?) { .\Asi2 } C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x30): undefined reference to Point::Point(float, float)' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x50): undefined reference to Point::Point(float, float)' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x79): undefined reference to Point::Point(Point&)' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0x8c): undefined reference to Point::calculateDistance(Point)' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xb6): undefined reference to Point::~Point()' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xc5): undefined reference to Point::~Point()' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xcf): undefined reference to Point::~Point()' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xdf): undefined reference to Point::~Point()' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xed): undefined reference to Point::~Point()' C:\Users\Luke\AppData\Local\Temp\ccOeBG8R.o:Asi2.cpp:(.text+0xfb): more undefined references to Point::~Point()' follow collect2.exe: error: ld returned 1 exit status

  • in the command line, "g++ ..." there is no mention of Point.cpp, so it will not be compiled this is why you have these undefined references. You have to add it , see https://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files – Olivier Sohn Sep 30 '20 at 22:57
  • The correct configuration of Visual Studio Code trips up a lot of people. Are simpler options available to you? You really don't want to have to learn the tricks of one of the trickiest programming languages at the same time you're learning the tricks of one of the trickier development tools because until you're familiar with at least one you'll have a hard time determining which is tricking you. – user4581301 Sep 30 '20 at 23:04
  • BTW `get(void)` is C style. C++ style does not include the `void` – Khouri Giordano Sep 30 '20 at 23:07
  • My Prof made it a requirement to use Visual Studio Code for the course and we are just getting into OOP approach with C++. I would have used something im much more familiar with if I was allowed to. – Luke Bowden Sep 30 '20 at 23:36
  • @OlivierSohn thats good to know, but I'm guessing I add that in the Terminal before I compile and run? Sorry it's such a basic question. I'm still learning how to use Visual Studio Code and it's caused nothing but headaches for me so far. – Luke Bowden Sep 30 '20 at 23:48
  • So it seems I can also solve the issue by adding #include "Point.cpp" at the top of my driver file (Asi2). Though as far as I'm aware that is bad practice for C++. Correct? – Luke Bowden Oct 01 '20 at 00:07
  • @LukeBowden I thought you were writing the command line, but if the command line is written by visual studio code, then your project in studio code is not well configured, and you need to add the Point.cpp file to the project, as a "file that needs to be compiled". Regarding including the file at the top of the other file, that is a bit like unitybuild, you can read this about it : https://stackoverflow.com/questions/847974/the-benefits-disadvantages-of-unity-builds – Olivier Sohn Oct 01 '20 at 18:22
  • @LukeBowden Yes. Including cpp files usually opens up [a whole can of worms](https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header). – user4581301 Oct 02 '20 at 00:01

0 Answers0