I am receiving the build fail error of "linker command failed with exit code 1" I have checked other forum posts that talk about duplicate function calls that confuse the linker. I don't have very much experience with C++ so i would love some help!
ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
LINE.H
#include <stdio.h>
#include <string>
using namespace std;
class RuntimeException { // generic run-time exception
private:
string errorMsg;
public:
RuntimeException(const string& err) { errorMsg = err; }
string getMessage() const { return errorMsg; }
};
// All that is needed for the special exceptions is the inherited constructor and method.
class EqualLines: public RuntimeException
{
public:
EqualLines(const string& err)
: RuntimeException(err) {}
};
class ParallelLines: public RuntimeException
{
public:
ParallelLines(const string& err)
: RuntimeException(err) {}
};
class Line {
public:
Line(double slope, double y_intercept): a(slope), b(y_intercept) {};
double intersect(const Line L) const throw(ParallelLines,
EqualLines);
double getSlope() const {return a;};
double getIntercept() const {return b;};
private:
double a;
double b;
};
LINE.CPP
#include "line.h"
#include <iostream>
double Line::intersect(const Line L) const throw(ParallelLines,
EqualLines)
{
if (this->a == L.a && this->b == L.b)
{
throw "EqualLines";
}
else if (this->a == L.a)
{
throw "ParallelLines";
}
return (L.b - this->b) / (this->a - L.a);
}
MAIN.CPP
#include "line.cpp"
#include <iostream>
int main()
{
Line L(2.0, 4.0);
Line K(3.0, 5.0);
try
{
if (L.getSlope() == K.getSlope() && L.getIntercept() == K.getIntercept())
throw EqualLines("The lines are equal: infinite intersection");
else if (L.getSlope() == K.getSlope())
throw ParallelLines("The lines are parallel: no intersection");
}
catch(EqualLines& zde)
{
}
catch(ParallelLines& zde)
{
}
cout << "Line 1: Y = " << L.getSlope() << "x + " << L.getIntercept();
cout << "\n";
cout << "Line 2: Y = " << K.getSlope() << "x + " << K.getIntercept();
cout << "\n\n";
L.intersect(K);
return 0;
}
在您的主文件中,您应该包含.h
文件而不是.cpp
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.