C++重新定义

发布于 2024-12-05 00:14:05 字数 1295 浏览 0 评论 0原文

我试图实现一个函数类,但出现重新定义错误。我知道这很愚蠢,但有人可以帮忙吗?

===头文件===

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じ违心 2024-12-12 00:14:05

您有一个 Add 类,因此您需要创建它的实例,以便调用构造函数。

因此,在下面的情况下,a 是我们的Add 类的实例。

Add a(zz);

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 our Add class.

Add a(zz);
傲影 2024-12-12 00:14:05

您可以选择在声明中的变量名称两边加上括号。

int (i);

与因此

int i;

在您的情况下您正在声明一个名为 zz 且类型为 Add 的变量,并且名为 zz 的变量已经存在。您可能打算将 zz 作为参数传递给 Add 构造函数,但是您应该为变量指定一些名称:

Add adder(zz);

但是,我看不到该实例的使用位置根本不。


但如果您只想调用 Add 的构造函数而不声明变量,则可以在整个表达式两边加上括号:

(Add(zz)); //just calls Add::Add(Complex);

Welcome to C++ ;)

You can optionally put parenthesis around the variable name in a declaration.

int (i);

is the same as

int i;

So in your case you are declaring a variable named zz of type Add, and a variable named zz already exists. You probably meant to pass zz as an argument to Add constructor, but then you should give some name to the variable:

Add adder(zz);

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:

(Add(zz)); //just calls Add::Add(Complex);

Welcome to C++ ;)

小苏打饼 2024-12-12 00:14:05

Add(zz) 是什么意思?你认为这意味着什么?

Add(zz)实际上是一个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 object zz of type Add, i.e.

Add(zz);

is equivalent to

Add zz;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文