gpt4 book ai didi

c++ - 代码在 visual studio 中运行良好,但是当转移到 Unix 时我收到错误?

转载 作者:行者123 更新时间:2023-11-28 00:10:05 25 4
gpt4 key购买 nike

不确定为什么 Unix 出于某种原因不喜欢我的代码,该代码在 Visual Studio 上完美运行,我需要使用 shell 客户端提交作业,当我尝试这样做时,我收到这些错误。如果这不是最好的问题,我也很抱歉,我真的很困惑为什么这不起作用。以下是错误:

w6_in_lab.cpp: In function âint main()â:
w6_in_lab.cpp:22:11: error: no match for âoperator=â in âA = B.sict::Account::operator+(C)â
w6_in_lab.cpp:22:11: note: candidates are:
Account.h:15:12: note: sict::Account& sict::Account::operator=(sict::Account&)
Account.h:15:12: note: no known conversion for argument 1 from âsict::Accountâ to âsict::Account&â
Account.h:17:9: note: char* sict::Account::operator=(char*)
Account.h:17:9: note: no known conversion for argument 1 from âsict::Accountâ to âchar*â
w6_in_lab.cpp:23:7: warning: deprecated conversion from string constant to âchar*â [-Wwrite-strings]
w6_in_lab.cpp:25:10: error: no match for âoperator=â in âA = B.sict::Account::operator+=((* & C))â
w6_in_lab.cpp:25:10: note: candidates are:
Account.h:15:12: note: sict::Account& sict::Account::operator=(sict::Account&)
Account.h:15:12: note: no known conversion for argument 1 from âsict::Accountâ to âsict::Account&â
Account.h:17:9: note: char* sict::Account::operator=(char*)
Account.h:17:9: note: no known conversion for argument 1 from âsict::Accountâ to âchar*â

我对这些错误中的任何一个都不熟悉,因为我不太熟悉重载运算符,而且我从不使用 Unix 进行 C++ 赋值。

这是 .h 文件:

#ifndef SICT_ACCOUNT_H__
#define SICT_ACCOUNT_H__
#include <iostream>
#include <cstring>
#include <iomanip>
namespace sict{
class Account{
char _name[41];
double _balance;
public:
Account();
Account(double balance);
Account(const char name[], double balance);
Account(const char name[]);
Account& operator=(Account& ls);
Account operator+=(Account& ls);
char* operator=(char* ls);
void display()const;
double getBal();
char* getName();

friend double operator+=(double& ls, Account& rs);
Account operator+(Account ls);

};

std::ostream& operator<<(std::ostream& ls, Account& rs);
};

#endif

.cpp 文件:

#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iomanip>
#include "Account.h"
using namespace std;
namespace sict{
Account::Account(){
_name[0] = 0;
_balance = 0;
}

Account::Account(double balance){
_name[0] = 0;
_balance = balance;
}

Account::Account(const char name[], double balance){
strncpy(_name, name, 40);
_name[40] = 0;
_balance = balance;
}

void Account::display()const{

for(int x = 0; x < 40; x++){
if(_name[x] == '\0')
x = 40;
else
cout << _name[x];
}

cout << ": $" << setprecision(2) << fixed << _balance;

}

Account Account::operator+(Account ls) {

return ls._balance + _balance;
}

double operator+=(double& ls, Account& rs){
//cout << ls << endl;
//cout << rs._balance+ ls << endl;
return ls+=rs._balance;



//return rs._balance+=ls;
}

Account Account::operator+=(Account& ls){

return _balance+=ls._balance;

}

Account::Account(const char name[]){
strncpy(_name, name, 40);
}

char* Account::getName(){

return _name;
}

double Account::getBal(){

return _balance;
}

std::ostream& operator<<(std::ostream& ls, Account& rs){
rs.display();
return ls;
}

Account& Account::operator=(Account& ls){
if( !strcmp(ls._name,"") &&ls._balance > 0)
{
strcpy(_name, "Saving");
}

_balance = ls._balance;
//strcpy(_name, ls._name);

return *this;

}

char* Account::operator=(char* ls){

strcpy(_name, ls);

return _name;

}


}

最后是主要内容:

#include <iostream>
#include <string>
#include "Account.h"
using namespace sict;
using namespace std;

int main(){
Account A;
Account B("Saving", 10000.99);
Account C("Checking", 100.99);
double value = 0;
cout << A << endl << B << endl << C << endl << "--------" << endl;
A = B + C;
A = "Joint";
cout << A << endl << B << endl << C << endl << "--------" << endl;
A = B += C;
cout << A << endl << B << endl << C << endl << "--------" << endl;
value += A;
value += B;
value += C;
cout << "Total balance: " << value << endl;
return 0;
}

最佳答案

您的许多问题纯粹是学术问题。

采用Account&operator=()(和其他函数)应该采用const 参数:

Account& operator=( const Account& ls );

同样,当您说 A = "Joint" 时,您正试图分配一个 const 字符串,但您的运算符只接受一个可变字符串。修复其参数类型:

Account& operator=(const char* ls);

请注意,operator=() 仍应返回对 *this 的引用。

不幸的是,习惯这些错误的最好方法是将 VS 的错误报告调到最大并修复您的代码,这样您就不会出错。

希望这对您有所帮助。

关于c++ - 代码在 visual studio 中运行良好,但是当转移到 Unix 时我收到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com