我想實現一個函數類,但得到了重新定義的錯誤。我知道這很愚蠢,但任何人都可以請幫忙嗎?C++重新定義
===頭文件===
#ifndef _NS4_h
#define _NS4_h
#include <vector>
#include <list>
namespace NS4{
class complex{
double r, i;
public:
complex(double a=0, double b=0) : r(a), i(b) {};
complex operator+(complex c);
complex &operator+=(complex c);
complex &operator=(complex c);
};
// function class
class Add{
complex val;
public:
Add(complex c){ val = c; }
Add(double r, double i) { val = complex(r, i); }
void operator()(complex& c) const { c += val; }
};
void h(std::vector<complex> &aa, std::list<complex> ll, complex z);
}
#endif
cpp文件===
using namespace NS4;
void test9()
{
vector<complex> aa;
aa.push_back(complex(0,1));
aa.push_back(complex(0,2));
aa.push_back(complex(0,3));
list<complex> ll;
ll.push_back(complex(1,1));
ll.push_back(complex(1,2));
ll.push_back(complex(1,3));
complex zz(1,1);
// the following line is not working
// error C2371: 'zz' : redefinition; different basic types
Add(zz); // Add(complex(1,1)) is working.
h(aa,ll, zz);
}
你可能想使用'void operator()(const complex&c)const'因爲您沒有修改'c' – GWW