0

This is my code. I think the problem would be Makefile or main.cpp but I can't figure it out.

And this is the description for this codes.

Your Point class will define an n-dimensional point. Your Point should have two constructors: a "default" constructor takes one integer, n, and constructs a point of n dimensions at the origin and a second constructor that takes either a vector or an array of integers as an argument.

You should add a method that reports how many dimensions this Point object is. It should have getter and setter methods for each coordinate and two methods, Distance, to calculate the Euclidian distance between this Point and one other point, and Translate, which moves all of this Points coordinates by whatever integer value is passed to the method. Note that Distance and Translate are methods of the Point class.

Your main function should create at least three points, calculate the distance between them, and report their coordinates and the calculated distances between them to the user. Do not prompt the user for the coordinates of the Points that you create.

Your main function should then translate one of the Points that you've created by a positive amount, then report the new coordinates and calculated distances between the translated point and the other points that you created to the user.

It should then translate the same Point by a negative amount (this amount does not need to be the same as the positive amount), then report the new coordinates and calculated distances between the translated point and the other points that you created to the user.

main.cpp

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

int main()
{
    Point p1(3,9),p2(2,5),p3(3,5);
    
    cout<<"Distance between P1("<<p1.getXCordinate()<<","<<p1.getYCordinate()<<") and P2("<<p2.getXCordinate()<<","<<p2.getYCordinate()<<") is :"<<p1.distance(p2)<<endl;
    cout<<"Distance between P2("<<p2.getXCordinate()<<","<<p2.getYCordinate()<<") and P3("<<p3.getXCordinate()<<","<<p3.getYCordinate()<<") is :"<<p2.distance(p3)<<endl;
    cout<<"Distance between P3("<<p3.getXCordinate()<<","<<p3.getYCordinate()<<") and P1("<<p1.getXCordinate()<<","<<p1.getYCordinate()<<") is :"<<p3.distance(p1)<<endl;
    
    p1.translate(2);
    p2.translate(6);
    p3.translate(8);
    cout<<"\nAfter Translation :"<<endl;
      cout<<"Distance between P1("<<p1.getXCordinate()<<","<<p1.getYCordinate()<<") and P2("<<p2.getXCordinate()<<","<<p2.getYCordinate()<<") is :"<<p1.distance(p2)<<endl;
    cout<<"Distance between P2("<<p2.getXCordinate()<<","<<p2.getYCordinate()<<") and P3("<<p3.getXCordinate()<<","<<p3.getYCordinate()<<") is :"<<p2.distance(p3)<<endl;
    cout<<"Distance between P3("<<p3.getXCordinate()<<","<<p3.getYCordinate()<<") and P1("<<p1.getXCordinate()<<","<<p1.getYCordinate()<<") is :"<<p3.distance(p1)<<endl;

    p1.translate(-3);
    p2.translate(-4);
    p3.translate(-9);    
    
    cout<<"\nAfter Translation :"<<endl;
  cout<<"Distance between P1("<<p1.getXCordinate()<<","<<p1.getYCordinate()<<") and P2("<<p2.getXCordinate()<<","<<p2.getYCordinate()<<") is :"<<p1.distance(p2)<<endl;
    cout<<"Distance between P2("<<p2.getXCordinate()<<","<<p2.getYCordinate()<<") and P3("<<p3.getXCordinate()<<","<<p3.getYCordinate()<<") is :"<<p2.distance(p3)<<endl;
    cout<<"Distance between P3("<<p3.getXCordinate()<<","<<p3.getYCordinate()<<") and P1("<<p1.getXCordinate()<<","<<p1.getYCordinate()<<") is :"<<p3.distance(p1)<<endl;

    return 0;
 }

Point.cpp

#include <iostream>
#include <cmath>
using namespace std;
#include "point.h"
#include "main.cpp"

    Point::Point()
    {
        this->cordinateX=0;
        this->cordinateY=0;
    }
      Point::Point(int x,int y)
      {
          this->cordinateX=x;
          this->cordinateY=y;
      }
      int Point::getXCordinate()
      {
          return cordinateX;
      }
      int Point::getYCordinate()
      {
          return cordinateY;
      }
      
      
      void Point::setYCordinate(int x)
      {
          this->cordinateY=x;
      }
      void Point::setXCordinate(int x)
      {
          this->cordinateX=x;
      }
      double Point::distance(Point p)
      {
         int dx = cordinateX - p.cordinateX;
        int dy = cordinateY - p.cordinateY;
        return sqrt(dx * dx + dy * dy);

      }
      void Point::translate(int val)
      {
          this->cordinateX-=val;
          this->cordinateY-=val;
      }

point.h

#ifndef POINT_H
#define POINT_H

class Point
{
    private:
      int cordinateX;
      int cordinateY;

public:
    Point();
      Point(int x,int y);
      int getXCordinate();
      int getYCordinate();
      void setXCordinate(int x);
      void setYCordinate(int y);
      double distance(Point p);
      void translate(int val);
};
#endif

Makefile

CXX = g++  # the compiler
CXXFLAGS = -std=c++11 -Wall

# runs for "make all"
all: main

main: main.o point.o
    $(CC) $(CFLAGS) -o main main.o point.o

main.o: main.cpp
    $(CC) $(CFLAGS) -c main.cpp

point.o: Point.cpp
    $(CC) $(CFLAGS) -c Point.cpp

clean:
    rm -f main

Error

clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

I'm trying to compile with 'make all' command but it keep getting error. every other commands are work now.

  • 1
    please include the complete error message – pm100 Jan 28 '23 at 01:35
  • English: **coordinate**. – Joop Eggen Jan 28 '23 at 01:42
  • There's no reason this question should have been closed. The problem is with your Makefile. You declare your compiler as `CXX` and your flags as `CXXFLAGS`, but you're using `CC` and `CFLAGS` instead. Switch out the variables and you should be fine. You also have to make sure your object files are capitalized as well. Each instance of `point.o` should be changed to `Point.o` to match the file name. – rburmorrison Jan 28 '23 at 01:46
  • @rburmorrison - file names make no difference – pm100 Jan 28 '23 at 01:50
  • While the makefile is likely *an* issue (and a glaring one at that. Take the time to define specific variables, and then never use them?), the bigger one is `#include "main.cpp"`. Aside from making zero sense (main.cpp needs to know about stuff, the stuff never needs to know about the main function), **never** include cpp files. – sweenish Jan 28 '23 at 02:08
  • @pm100 Maybe my make game is weak, but the point.o rule would not result in point.o, but Point.o. Unless you're talking about case insensitivity. Naming issues like this shouldn't even be a thing if OP made better use of make variables, or better yet, threw out their makefile and used cmake instead. – sweenish Jan 28 '23 at 02:15
  • @pm100 The case sensitivity matters on Linux, which is where I tested. You're probably right if you're testing on Windows. @sweenish is correct about the file name producing `Point.o`. – rburmorrison Jan 28 '23 at 02:26

1 Answers1

-1

do not include main.cpp here

#include "point.h"
#include "main.cpp" <<<=====

this is not going to cause the 'missing symbol' error. But it will cause 'multiply defined symbol' errors. I assume you added that include as a way to work round the other error.

rreading your code you are probably getting 'sqrt' as the missing symbol. You need to add the -lm flag to the link command - see https://man7.org/linux/man-pages/man3/sqrt.3.html ("if all else fails, read the manual")

main: main.o point.o
    $(CC) $(CFLAGS) -o main main.o point.o -lm
pm100
  • 48,078
  • 23
  • 82
  • 145
  • I have never had to manually link the math library. And that man page is also for math.h, which is not cmath. – sweenish Jan 28 '23 at 02:12