我已经实现了一个工作正常的类复合体。
http://coliru.stacked-crooked.com/a/b6bff3884e8e2460
现在我正在尝试生成一个头文件:
//
// cplex.h
//
//
//
//
//
#ifndef cplex_h
#define cplex_h
#include <iostream>
class complex
{
friend complex operator-(const complex& c);
friend complex operator +(const complex& c1 , const complex& c2 );
friend complex operator +(const complex& c1 , const double& db );
friend complex operator +(const double& db , const complex& c1 );
friend complex operator-(const complex& c1, const double& d);
friend complex operator-(const complex& c1 , const complex& c2 );
friend complex operator-(const double& d ,const complex& c1);
friend complex operator *(const complex& c1 , const complex& c2 );
friend complex operator *( const complex& lhs , const double& rhs);
friend complex operator *( const double& lhs , const complex& rhs);
friend complex operator /(const complex& lhs, const complex& rhs );
friend complex operator /(const complex& lhs, const double& rhs );
friend complex operator /(const double& lhs, const complex& rhs );
friend std::istream& operator>>(std::istream &is , complex &cp );
private:
double re;
double im;
public:
// constructors
complex (); // default constructor;
constexpr complex(const double real,const double img);
complex(double real);
complex(const complex& rhs);
// member functions;
double real() const;
double imag() const;
complex& operator = (const complex& );
complex& operator =(const double &);
complex& operator += (const complex&);
complex& operator +=( const double& other );
complex& operator -=(const double& db );
complex& operator -= (const complex&);
complex& operator *=(const double& db);
complex& operator *= (const complex&);
complex& operator /=(const double& db);
complex& operator /= (const complex& );
};
std::ostream& operator <<(std::ostream &os ,const complex &cp );
#endif /* cplex_h */
好吧最后我想做一些cxxtest并调用默认构造函数(complex x;
)
我收到一条错误消息:
重复符号_main: /var/folders/08/s_gsvpm577vgftp3tj2_47hr0000gn/T//ccbn1AAH.o complex.o ld:1个用于体系结构的重复符号x86_64 collect2:error:ld返回1退出状态
#include "cplex.h"
#include <cxxtest/TestSuite.h>
class cptests : public CxxTest::TestSuite
{
public:
void test1(void)
{
complex x;
TS_ASSERT_EQUALS(x.real(), 0);
TS_ASSERT_EQUALS(x.imag(), 0)
}
};
另一方面,如果我尝试调用另一个构造函数
复杂x(5.0,5.0);
而不是
复杂x;
我收到错误消息:
In file included from cptests.h:9:0,
from runner.cpp:23:
cplex.h:45:15: warning: inline function 'constexpr complex::complex(double, double)' used but never defined
constexpr complex(const double real,const double img);
^
Undefined symbols for architecture x86_64:
"complex::complex(double, double)", referenced from:
cptests::test1() in ccpZuOPa.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
我知道我的构造函数工作正常:我已在其中测试过它们:class complex。
试图解决这些问题我有几个问题:
我的constexpr构造函数是否必须在我的头类中定义?即使它已经在类复杂中定义了。
这些在类复杂中定义的函数是否需要在头文件的类中声明:在我的例子中,class:complex?那么非成员函数呢?
答案 0 :(得分:1)
第一条错误消息是由于名为_main
的重复符号。这意味着其中之一;
定义main()
的编译单元在构建中列出两次(例如,链接时指定了两次目标文件);或
main()的定义在头文件中,它包含在两个编译单元中。这也可能表示您在标题中有#include
个源文件。
第二条错误消息(您在编辑问题之前将其删除)意味着编译器认为您已使用三个参数调用构造函数,而不是两个(与您的描述相反)。我猜这是一个错字,所以你认为你键入了complex x(5.0,5.0)
但实际上输入了complex x(5.0, 5,0)
,即,
(逗号)而不是.
(小数点)。< / p>
关于未定义构造函数的警告反映了编译器需要查看所有constexpr
函数的定义,否则它无法在编译时对它们进行求值。