English 中文(简体)
没有提到我的班子? C++ Beginner
原标题:Undefined reference to my classes? C++ Beginner

a. 与OOP i m接触,试图打上点级(斜线2,× & y)和线级(分两点)。

现在我开始建设主人。 c 出现错误。

<>m>未明确提及“点:点(float,lick)”

" undefined reference to `Line::Line(Point, Point) "

At a loss as to why, perhaps you could take a brief look at my files? It d be much appreciated!

Main.cpp

#include "Point.hpp"
#include "Line.hpp"
#include <iostream>

using namespace std;

int main()
{
    Point p1(2.0f, 8.0f); // should default to (0, 0) as specified
    Point p2(4.0f, 10.0f);  // should override default

    p1.setX(17);


    if ( p1.atOrigin() && p2.atOrigin() )
        cout << "Both points are at origin!" << endl;
    else
    {
        cout << "p1 = ( " << p1.getX() << " , " << p1.getY() << " )" <<endl;
        cout << "p2 = ( " << p2.getX() << " , " << p2.getY() << " )" <<endl;
    }

    Line line(p1, p2);
    Point midpoint = line.midpoint();
    cout << "p1 = ( " << midpoint.getX() << " , " << midpoint.getY() << " )" <<endl;
    return 0;
}

<>strong>Line.hpp

#ifndef _LINE_HPP_
#define _LINE_HPP_

#include "Point.hpp"

class Line{
public:
    Line(Point p1, Point p2);
    //void setp1(Point p1);
    //void setp2(Point p2);
    //Point getp1 finish

    Point midpoint();
    int length();

private:
    int _length;
    Point _midpoint;
    Point _p1, _p2;
};

#endif

Line.cpp

#include "Line.hpp"
#include <math.h>

Line::Line(Point p1, Point p2) : _p1(p1), _p2(p2)
{
}
Point Line::midpoint()
{
    _midpoint.setX() = (_p1.getX()+ _p2.getX()) /2;
    _midpoint.setY() = (_p1.getY()+ _p2.getY()) /2;
}
int Line::length()
{
    //a^2 + b^2 = c^2

    _length = sqrt( ( (pow( _p2.getX() - _p1.getX(), 2 ))
                     +(pow( _p2.getY() - _p1.getY(), 2 )) ) );
}

<>strong>Point.hpp

#ifndef _POINT_HPP_
#define _POINT_HPP_

class Point {
public:
    Point( float x = 0, float y = 0);
    float getX() const;
    float getY() const;
    void setX(float x = 0);
    void setY(float y = 0);
    void setXY(float x = 0, float y = 0);
    bool atOrigin() const;

private:
    float _x, _y;

};

#endif

<>strong>Point.cpp

#include "Point.hpp"

Point::Point(float x, float y) : _x(x), _y(y)
{
}

float Point::getX() const
{
    return _x;
}
float Point::getY() const
{
    return _y;
}
void Point::setX(float x)
{
    //if (x >= 0 &&
    _x = x;
}
void Point::setY(float y)
{
    //might want to check
    _y = y;
}
void Point::setXY(float x , float y )
{
    setX(x);
    setY(y);
}
bool Point::atOrigin() const
{
    if ( _x == 0 && _y == 0)
        return true;

    return false;
}
最佳回答

在C++中,你不仅必须汇编<代码>main.cpp,而且还要汇编<代码>Line.cpp和Point.cpp文档。 然后,当你们把所有文件汇编成物体档案时,你必须link<>>。 这一点由 Java等其他一些语文自动处理。

如何做到这一点的确切指示将取决于你利用哪些发展环境。

问题回答

您的点子(Cppn t)正在整理或交给联系人,试图将其纳入你的建筑。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?