Reputation: 373
I have implemented a class complex which works fine.
http://coliru.stacked-crooked.com/a/b6bff3884e8e2460
Now I am trying to produce a header file:
//
// 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 */
ok so finally I want to do some cxxtest and I call the default constructor (complex x;
)
I get an error message :
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)
}
};
on the other hand if I try to call another constructor
complex x(5.0,5.0);
instead of
complex x;
I get the error message:
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
I know my constructors work fine: I have tested them within : class complex.
trying to fix these problems I have a couple of questions :
Does my constexpr constructor have to be defined within my header class? even if it already was defined in class complex.
Does these functions, defined in class complex, need to be declared inside a class in the header file: in my case the class: complex? what about nonmember functions ?
Upvotes: 0
Views: 84
Reputation: 36597
The first error message is because of a duplicated symbol named _main
. That means one of;
the compilation unit which defines main()
is listed twice in the build (e.g. the object file is specified twice when linking); or
the definition of main() is in a header file, which is included in two compilation units. This may also indicate you have #include
d a source file within a header.
The second error message (the one you had before you edited your question to remove it) means the compiler thinks you have invoked the constructor with three arguments, not two (contrary to your description). My guess is a typo, so you think you have typed complex x(5.0,5.0)
but have actually typed complex x(5.0, 5,0)
i.e. a ,
(comma) instead of a .
(decimal point).
The warning about undefined constructors reflects the compiler needing visibility of the definition of all constexpr
functions, otherwise it cannot evaluate them at compile time.
Upvotes: 2