首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

error C2679 数据结构与算法第三版习题1.15,该如何处理

2012-03-18 
error C2679数据结构与算法第三版习题1.15《数据结构与算法分析 第三版 C++描述》习题1.15运行时候出现如下

error C2679 数据结构与算法第三版习题1.15
《数据结构与算法分析 第三版 C++描述》习题1.15
运行时候出现如下问题:


1>f:\c workspace\111030_chapter1answersheet_dsaa\111030_chapter1answersheet_dsaa\answersheet_dsaa.cpp(158) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Employee' (or there is no acceptable conversion)
1> d:\software\microsoft visual studio 2008\vc\include\xstring(914): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\software\microsoft visual studio 2008\vc\include\xstring(919): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\software\microsoft visual studio 2008\vc\include\xstring(924): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::string, const Employee)'
1>Build log was saved at "file://f:\C Workspace\111030_Chapter1AnswerSheet_DSAA\111030_Chapter1AnswerSheet_DSAA\Debug\BuildLog.htm"

这应该是一个很简单的问题。本人新手,实在是看不懂!望大侠们指教!


自己写的源程序

#include <string>
#include <iostream>
#include <vector>
using namespace std;

string getLast( const string & nm );

class Employee
{
public:
void setValue( const string & n, double s )
{
name = n;
salary = s;
}

const string & getName() const
{
return name;
}

private:
string name;
double salary;
};

/*
const Employee & Employee::operator =(const Employee &rhs)
{
if ( this != &rhs )
{
name = *rhs.name;
salary = *rhs.salary;
}

return *this;
}
*/

template <typename Object, typename Comparator>
const Object & findMax( const vector<Object> & arr, Comparator isLessThan )
{
int maxIndex = 0;

for ( int i = 1; i < arr.size(); i++ )
if ( isLessionThan( arr[ maxIndex ], arr[ i ] ) )
maxIndex = i;

return arr[ maxIndex ];
}

class EmployeeLastNameCompare
{
public:
bool operator() ( const Employee & lhs, const Employee & rhs ) const
{
return getLast( lhs.getName() ) < getLast( rhs.getName() );
}

};

string getLast( const string & name )
{
string last;
int blankPosition = name.find( " " );

last = name.substr( blankPosition + 1, name.size() );

return last;
}

int main()
{
vector<Employee> v(3);
string str = "";

v[0].setValue( "George Bush", 400000.00 );
v[1].setValue( "Bill Gates", 2000000000000.00 );
v[2].setValue( "Dr. Phil", 1300000000.00 );
str = ( findMax( v, EmployeeLastNameCompare() ) );
cout << str << endl;

return 0;
}

[解决办法]
改好了,有问题加我Q: 506341588

C/C++ code

#include <string>#include <iostream>#include <vector>using namespace std;string getLast( const string & nm );class Employee{public:    void setValue( const string & n, double s )    {        name = n;        salary = s;    }        const string& getName() const    {         return name;    }        const double& getSalary() const    {         return salary;    }        const Employee& operator =(const Employee &rhs);    private:    string name;    double salary;};ostream& operator << (ostream &out, const Employee &rhs){    out << rhs.getName() << " (" << rhs.getSalary() << ")";    return out;}const Employee & Employee::operator =(const Employee &rhs){    if ( this != &rhs )    {        name = rhs.name;        salary = rhs.salary;    }        return *this;}template <typename Object, typename Comparator>const Object & findMax( const vector<Object> & arr, Comparator cmp ){    int maxIndex = 0;        for ( int i = 1; i < arr.size(); i++ )        if ( cmp.isLessionThan( arr[ maxIndex ], arr[ i ] ) )            maxIndex = i;                return arr[ maxIndex ];}class EmployeeLastNameCompare{public:    bool isLessionThan( const Employee & lhs, const Employee & rhs ) const    {        return getLast( lhs.getName() ) < getLast( rhs.getName() );    }    };string getLast( const string & name ){    string last;    int blankPosition = name.find( " " );        last = name.substr( blankPosition + 1, name.size() );        return last;}int main(){    vector<Employee> v(3);        v[0].setValue( "George Bush", 400000.00 );    v[1].setValue( "Bill Gates", 2000000000000.00 );    v[2].setValue( "Dr. Phil", 1300000000.00 );    cout << findMax( v, EmployeeLastNameCompare() ) << endl;        return 0;}
[解决办法]
C/C++ code
#include <string>#include <iostream>#include <vector>using namespace std;string getLast( const string & nm );class Employee{public:    void setValue( const string & n, double s )    {        name = n;        salary = s;    }        const string& getName() const    {         return name;    }        const double& getSalary() const    {         return salary;    }        const Employee& operator =(const Employee &rhs);    private:    string name;    double salary;};ostream& operator << (ostream &out, const Employee &rhs){    out << rhs.getName() << " (" << rhs.getSalary() << ")";    return out;}const Employee & Employee::operator =(const Employee &rhs){    if ( this != &rhs )    {        name = rhs.name;        salary = rhs.salary;    }        return *this;}template <typename Object, typename Comparator>const Object & findMax( const vector<Object> & arr, Comparator cmp ){    int maxIndex = 0;        for ( int i = 1; i < arr.size(); i++ )        if ( cmp.isLessionThan( arr[ maxIndex ], arr[ i ] ) )            maxIndex = i;                return arr[ maxIndex ];}class EmployeeLastNameCompare{public:    bool isLessionThan( const Employee & lhs, const Employee & rhs ) const    {        return getLast( lhs.getName() ) < getLast( rhs.getName() );    }    };string getLast( const string & name ){    string last;    int blankPosition = name.find( " " );        last = name.substr( blankPosition + 1, name.size() );        return last;}int main(){    vector<Employee> v(3);        v[0].setValue( "George Bush", 400000.00 );    v[1].setValue( "Bill Gates", 2000000000000.00 );    v[2].setValue( "Dr. Phil", 1300000000.00 );    cout << findMax( v, EmployeeLastNameCompare() ) << endl;        return 0;} 

热点排行