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);
}
I am trying to implement a function class, but got a error of redefinition. I know it is stupid, but can anyone please help?
=== header file ===
#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
=== Part of the cpp file ===
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有一个 Add 类,因此您需要创建它的实例,以便调用构造函数。
因此,在下面的情况下,
a
是我们的Add
类的实例。You have an Add class, so you need to create an instance of it, in order to call the constructor.
So in the below case,
a
is an instance of ourAdd
class.您可以选择在声明中的变量名称两边加上括号。
与因此
在您的情况下您正在声明一个名为
zz
且类型为Add
的变量,并且名为zz
的变量已经存在。您可能打算将zz
作为参数传递给Add
构造函数,但是您应该为变量指定一些名称:但是,我看不到该实例的使用位置根本不。
但如果您只想调用
Add
的构造函数而不声明变量,则可以在整个表达式两边加上括号:Welcome to C++ ;)
You can optionally put parenthesis around the variable name in a declaration.
is the same as
So in your case you are declaring a variable named
zz
of typeAdd
, and a variable namedzz
already exists. You probably meant to passzz
as an argument toAdd
constructor, but then you should give some name to the variable:However, I don't see where that instance is used at all.
But if you just want to invoke the constructor of
Add
without declaring a variable, you can put parenthesis around the whole expression:Welcome to C++ ;)
Add(zz)
是什么意思?你认为这意味着什么?Add(zz)
实际上是一个Add
类型的对象zz
的声明,即相当于
你已经定义了
zz 之前,这就是您收到重新定义错误的原因。这里并不奇怪。
如果不知道
Add(zz)
行想要做什么,就无法为您提供进一步的帮助。What is
Add(zz)
supposed to mean? What do you think it means?Add(zz)
is actually a declaration of objectzz
of typeAdd
, i.e.is equivalent to
You have already defined
zz
before, which is why you get the redefinition error. No surprise here.There's no way to help you further without knowing what you were trying to do by that
Add(zz)
line.