2

I am creating a simple C++ project of the relationship between points and a circle in VS code (macOS Version 12.1). My code worked well if I declared and defined Point and Circle classes within the main script. However, when I declared and defined classes in .cpp and .h separately, I got the following error:

Undefined symbols for architecture x86_64:
  "Point::getX()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Point::getY()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Point::setX(int)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
  "Point::setY(int)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
  "Circle::getR()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Circle::setR(int)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
  "Circle::getCenter()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Circle::setCenter(Point)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my tasks.json file:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /usr/bin/clang"
        }
    ]
}

My previous compiling without self-defined headers was all good. But I don't see any mistakes in my code, so I guess it's a setting or system issue. Appreciate your kind help!


This is my Point.hfile:

#pragma once 
#include <iostream>
using namespace std;

class Point
{
private:
    int m_X, m_Y;
public:
    void setX(int x);
    int getX();
    void setY(int y);
    int getY();
};

This is Point.cppfile:

#include "Point.h"
void Point::setX(int x){ m_X = x; } 
int Point::getX(){ return m_X; };
void Point::setY(int y){ m_Y = y; }
int Point::getY(){ return m_Y; };

This is Circle.hfile:

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

class Circle
{
private:
    int m_R;
    Point m_Center;   
public:
    void setR(int r);
    int getR();
    void setCenter(Point center); 
    Point getCenter(); 
};

This is Circle.cppfile:

#include "Circle.h"
void Circle::setR(int r){ m_R = r; }
int Circle::getR(){ return m_R; };
void Circle::setCenter(Point center){ m_Center = center; }  
Point Circle::getCenter() { return m_Center; };   

Here is my main function:

#include "Point.h"
#include "Circle.h"
using namespace std;

void isInCircle(Circle &c, Point &p)
{
    int distance = 
    (c.getCenter().getX() - p.getX())*(c.getCenter().getX() - p.getX())
     + (c.getCenter().getY() - p.getY())*(c.getCenter().getY() - p.getY());
    int rDistance = c.getR() * c.getR();
    if (distance == rDistance) { cout<<"Point is on the circle."<<endl;}
    else if (distance > rDistance) { cout<<"Point is off the circle."<<endl;}
    else { cout<<"Point is in the circle."<<endl;}
}

int main(){
    Circle circle;
    circle.setR(10);
    Point center;
    center.setX(10);
    center.setY(0);
    circle.setCenter(center);

    Point myPoint;
    myPoint.setX(10);
    myPoint.setY(11);
    isInCircle(circle, myPoint);

    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Cyan
  • 319
  • 2
  • 8
  • 1
    how are you compiling your code? You need to link all your c++ files into your final executable – Alan Birtles Jan 25 '22 at 13:55
  • If you are using the default behavior of VSCode it will only compile the active file into your executable. The documentation explains this and tells you what change to make to your tasks.json to allow for more than 1 source file: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jan 25 '22 at 13:57
  • @drescherjm I tried [this](https://stackoverflow.com/questions/61170382/vs-code-cannot-compile-mutiple-c-fileswsl) and [this](https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files). But it's still not working. I have edited my question and attached my `tasks.json` file – Cyan Jan 25 '22 at 14:24
  • Do you have some other extension active like code-runner? – drescherjm Jan 25 '22 at 14:28
  • yes coder runner is installed and enabled @drescherjm – Cyan Jan 25 '22 at 14:29
  • After looking at your errors more carefully I just saw that. There are completely different instructions for supporting more than 1 file with code-runner. It does not use the tasks.json. I am not very familiar with the fix for code runner so I can't help with that. – drescherjm Jan 25 '22 at 14:30
  • Here is a related question for code-runner: [https://stackoverflow.com/questions/59474537/code-runner-configuration-for-running-multiple-cpp-classes-in-vscode](https://stackoverflow.com/questions/59474537/code-runner-configuration-for-running-multiple-cpp-classes-in-vscode) – drescherjm Jan 25 '22 at 14:33

0 Answers0