Luke Bowden
Luke Bowden

Reputation: 1

C++ Linking and Compiling troubles in Visual Studio Code

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

Upvotes: 0

Views: 57

Answers (0)

Related Questions