首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于运算法重载的有关问题~

2013-10-24 
关于运算法重载的问题~!#include iostreamusing namespace stdclass Test{public:Test(int a0){Test::

关于运算法重载的问题~!

#include <iostream>
using namespace std;


class Test
{
public:
Test(int a=0)
{
Test::a=a;
}
Test(Test &temp)
{
cout<<"载入拷贝构造函数"<<"|"<<temp.a<<endl;
Test::a=temp.a;
}

~Test()
{
cout<<"载入析构函数!"<<endl;
cin.get();
}

Test operator +(Test& temp2)
{
cout<<this->a<<endl;
Test result(this->a+temp2.a);
    return result;
}
Test& operator ++()
{
this->a++;
return *this;
}
public:
int a;
};

int main(int argc,char* argv[])
{
Test a(100);
Test c=a+a;
cout<<c.a<<endl;
c++;
cout<<c.a<<endl;
++c;
cout<<c.a<<endl;
++(++c);
cout<<c.a<<endl;
cin.get();
}


ibm@ubuntu:~$ g++ -Wall 运算符重载.cpp 
运算符重载.cpp: 在函数‘int main(int, char**)’中:
运算符重载.cpp:42:11: 错误: 对‘Test::Test(Test)’的调用没有匹配的函数
运算符重载.cpp:42:11: 附注: 备选是:
运算符重载.cpp:12:3: 附注: Test::Test(Test&)
运算符重载.cpp:12:3: 附注:   no known conversion for argument 1 from ‘Test’ to ‘Test&’
运算符重载.cpp:8:3: 附注: Test::Test(int)
运算符重载.cpp:8:3: 附注:   no known conversion for argument 1 from ‘Test’ to ‘int’
运算符重载.cpp:44:3: 错误: 没有为后缀‘++’声明‘operator++(int)’ [-fpermissive]
ibm@ubuntu:~$ 


新手C++程序设计语言才看到第六章,高手给看下 这些错误是咋回事,多谢了~!!
[解决办法]
1)
a+a ==>Test operator +(Test& temp2)
返回值 Test类型,是个临时对象,和拷贝构造函数Test(Test &temp)不能匹配。
Test 和 Test & 不匹配,
Test c=a+a; 就产生以下编译错误和提示信息:

运算符重载.cpp: 在函数‘int main(int, char**)’中:
运算符重载.cpp:42:11: 错误: 对‘Test::Test(Test)’的调用没有匹配的函数
运算符重载.cpp:42:11: 附注: 备选是:
运算符重载.cpp:12:3: 附注: Test::Test(Test&)
运算符重载.cpp:12:3: 附注:   no known conversion for argument 1 from ‘Test’ to ‘Test&’
运算符重载.cpp:8:3: 附注: Test::Test(int)
运算符重载.cpp:8:3: 附注:   no known conversion for argument 1 from ‘Test’ to ‘int’

改成
 Test(const Test &temp)

类型就匹配了。

拷贝构造函数的一般形式就是:
Test(const Test &temp)

2) c++; 需要后缀++运算符
而你的程序没有重载
运算符重载.cpp:44:3: 错误: 没有为后缀‘++’声明‘operator++(int)’ [-fpermissive]整理以下:

后缀++运算符重载的一般形式

Test operator ++(int)
{
    // 2.1)可以这么做
       Test ret(a++);
    // 2.2)也可以这样 Test ret(a);  ++a; 
    // 代码其实可以任意写,不过后缀++的语义,是对象自增,返回自增前的值 ...... 
    // 符合运算符的语义的运算符重载,才是你所需要的。
     return ret;
}


整理一下:

class Test{
//
public:
       // Test(int a=0)
       // {
       //     Test::a=a;//这样可能没有错误,不过很少人这样写。
       // }
       //多半是这样写: 
       Test(int a=0):a(a){}
       //或者这样写: 
       //  Test(int a = 0)
       // {
       //     this->a = a;//这样是为了区分成员变量和形参,
       //      一般来说,形参不要和成员变量同名,以免遮蔽同名的成员变量。
       // }
//  拷贝构造函数,一般传递常量引用。    
Test(const Test &temp)
        {
            cout<<"载入拷贝构造函数"<<"
[解决办法]
"<<temp.a<<endl;
             //Test::a=temp.a; //这里加类名是多余的。
             a = temp.a; //这样写就成。
        }
 ~Test()
        {
            cout<<"载入析构函数!"<<endl;
            cin.get();


        }
Test& operator ++()
        {
            this->a++;
            return *this;
        }

Test& operator ++()
        {  Test ret(*this);
            ++ (*this);
            return ret;
        }
public://一般就用private:了,不过你这里要在main 里用到,马马虎虎,可以这么做。
       //类的成员变量,多数时候,都是 private 或者protected 的。
       int a;
};
int main(int argc,char* argv[])
{
    Test a(100);
    Test c=a+a;
    cout<<c.a<<endl;
    c++;
    cout<<c.a<<endl;
    ++c;
    cout<<c.a<<endl;
    ++(++c);
    cout<<c.a<<endl;
    cin.get();
    return 0; //C++ 要求带返回值的函数,必须返回一个值。
}
       

热点排行