gpt4 book ai didi

C++ 这里有一些错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:39 25 4
gpt4 key购买 nike

谁能帮我解决这个问题?我是新来的。欢迎任何帮助!

9.3.cpp: In constructor ‘Address::Address(int,std::string.int.std::string,std::String,std::String)’: 9.3.cpp:29:51:error:’this’ cannot be used as function

9.3.cpp: In member function ‘int Address::compareTo(const Address&)’: 9.3.cpp:39:26:error:’std::string’has no member named ‘compareTo’

#include<iostream>
using namespace std;

class Address {
int houseNumber;
string street;
int apartmentNumber;
string city;
string state;
string zipCode; // e.g., "47405-1234"
Address(int houseNumber,
string street,
// no apartmentNumber
string city,
string state,
string zipCode) {
this->houseNumber = houseNumber;
this->street = street;
this->city = city;
this->state = state;
this->zipCode = zipCode;
}
Address(int houseNumber,
string street,
int apartmentNumber,
string city,
string state,
string zipCode) {
this(houseNumber, street, city, state, zipCode);
this->apartmentNumber = apartmentNumber;
}
void print(void) {
std::cout << "Street: " << street << "\nCity: "
<< city << "\nState: " << state << "\nPostal Code: " << zipCode;
}

int compareTo(const Address &a) {
// same conventions as for Strings
return this->zipCode.compareTo(angel);
}
};

最佳答案

这一行:

this(houseNumber, street, city, state, zipCode);

你不能调用自己的构造函数,至少不能这样。 C++11 标准中的新功能是使用初始化列表来完成此操作:

Address(int houseNumber, 
string street,
int apartmentNumber,
string city,
string state,
string zipCode)
: Address(houseNumber, street, city, state, zipCode)
{
this->apartmentNumber = apartmentNumber;
}

如果您的编译器还不支持它,那么您必须从第一个构造函数复制代码,或者将公共(public)初始化放在一个单独的函数中,以便从两个构造函数调用。

关于C++ 这里有一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12276247/

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