Я реализовал комплекс классов, который отлично работает.Файлы заголовка, вызывающие проблемы
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;
) я получаю сообщение об ошибке :
duplicate symbol _main in: /var/folders/08/s_gsvpm577vgftp3tj2_47hr0000gn/T//ccbn1AAH.o complex.o ld: 1 duplicate symbol for architecture x86_64 collect2: error: ld returned 1 exit status
#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)
}
};
с другой стороны, если я пытаюсь вызвать другой конструктор
complex x(5.0,5.0);
вместо
complex 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 конструктор должны быть определены в моем классе заголовка? даже если он уже был определен в классовом комплексе.
Должны ли эти функции, определенные в классе сложными, быть объявлены внутри класса в файле заголовка: в моем случае класс: complex? как насчет функций, не являющихся членами?