如何解决类构造函数中的数据结构错误和 C++ 中类的不可访问性
我的代码中有一些问题,例如:类“romanType”不存在默认构造函数,并且 romanType::convertRoman_decimal(std::string) 无法访问。我做了我能做的,但我做不到。
这是完整的代码:
#include<iostream>
using namespace std;
#include <string>
class romanType
{
friend ostream& operator<< (ostream&,const romanType&);
friend istream& operator>> (istream&,romanType&);
protected:
string romanNum; //roman numeral
//constructor to set the Roman numeral
romanType(string);
//convert Roman numeral to decimal number
int convertRoman_decimal(string);
//convert decimal number to Roman numeral
string convertDecimal_roman(int);
};
//overload the stream insertion << operator
ostream& operator <<(ostream& os,const romanType& objR)
{
os << "The Roman numeral is: ";
os <<objR.romanNum;
//returns ostream object
return os;
}
//overload the stream extraction >> operator
istream& operator >> (istream& is,romanType& objR)
{
is >> objR.romanNum;
//returns istream object
return is;
}
//Constructor to set the Roman numeral
romanType:: romanType(string a)
{
romanNum = a;
}
//convert and store into the decimal number
int romanType:: convertRoman_decimal(string romanNum){
//assigning decimal values to the roman characters
enum romanChars
{
M = 1000,D = 500,C = 100,L = 50,X = 10,V = 5,I = 1
};
//variable to store the result decimal number
int decNo = D;
//loop runs for all the Roman characters
for(int j = 0; j < romanNum.size() - 1; j++)
{
char ch = romanNum[j]; //character j of the string
//check the Roman character and add corresponding
//decimal value,before adding check next Roman
//character,if its decimal value is greater:
//subtract its value from next’s decimal value
switch(ch)
{
//adding value of char to decimal number
case 'M':
decNo += M; //add value of ‘M’
break;
case 'D':
//if the character is ‘D’ and next is ‘M’
//subtract value of ‘D’ from ‘M’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'M'))
decNo += M - D;
else
decNo += D; //add value of ‘D’
break;
case 'C':
//if the character is ‘C’ and next is ‘M’
//subtract value of ‘C’ from ‘M’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'M'))
decNo += M - C;
//if the character is ‘C’ and next is ‘D’
//subtract value of ‘C’ from ‘D’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'D'))
decNo += D - C;
else
decNo += C; //add value of ‘C’
break;
case 'L':
//if the character is ‘L’ and next is ‘M’
//subtract value of ‘L’ from ‘M’
if((j+1 != romanNum.size()) && (romanNum[j+1] == 'M'))
decNo += M - L;
//if the character is ‘L’ and next is ‘D’
//subtract value of ‘L’ from ‘D’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'D'))
decNo += D - L;
//if the character is ‘L’ and next is ‘C’
//subtract value of ‘L’ from ‘C’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'C'))
decNo += C - L;
else
decNo += L; //add value of ‘L’
break;
case 'X':
//if the character is ‘X’ and next is ‘M’
//subtract value of ‘X’ from ‘M’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'M'))
decNo += M - X;
//if the character is ‘X’ and next is ‘D’
//subtract value of ‘X’ from ‘D’
if((j+1 != romanNum.size()) && (romanNum[j+1] == 'D'))
decNo += D - X;
//if the character is ‘X’ and next is ‘C
//subtract value of ‘X’ from ‘C’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'C'))
decNo += C - X;
//if the character is ‘X’ and next is ‘L’
//subtract value of ‘X’ from ‘L’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'L'))
decNo += L - X;
else
decNo += X; //add value of ‘X’
break;
case 'V':
//if the character is ‘V’ and next is ‘M’
//subtract value of ‘V’ from ‘M’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'M'))
decNo += M - V;
//if the character is ‘V’ and next is ‘D’
//subtract value of ‘V’ from ‘D’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'D'))
decNo += D - V;
//if the character is ‘V’ and next is ‘C’
//subtract value of ‘V’ from ‘C’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'C'))
decNo += C - V;
//if the character is ‘V’ and next is ‘L’
//subtract value of ‘V’ from ‘L’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'L'))
decNo += L - V;
//if the character is ‘V’ and next is ‘X’
//subtract value of ‘V’ from ‘X’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'X'))
decNo += X - V;
else
decNo += V; //add value of ‘V’
break;
case 'I':
//if the character is ‘I’ and next is ‘M’
//subtract value of ‘I’ from ‘M’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'M'))
decNo += M - I;
//if the character is ‘I’ and next is ‘D’
//subtract value of ‘I’ from ‘D’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'D'))
decNo += D - I;
//if the character is 'I' the next is 'L'
//substract value of 'I' from 'L'
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'C'))
decNo += C - I;
//if the character is ‘I’ and next is
//subtract value of ‘I’ from ‘L’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'L'))
decNo += L - I;
//if the character is ‘I’ and next is
//subtract value of ‘I’ from ‘X’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'X'))
decNo += X - I;
//if the character is ‘I’ and next is
//subtract value of ‘I’ from ‘V’
if((j+1 != romanNum.size()) &&
(romanNum[j+1] == 'V'))
decNo += V - I;
else
decNo += I; //add value of ‘I’
break;
}
}
return decNo; //returns the decimal number
}
//convert the decimal number to Roman numeral
string romanType:: convertDecimal_roman(int decNum)
{
//consider the maximum decimal value can be 1000
//variable to store the Roman numeral
string romanNo = "";
int digit; //used to store the individual digits
//if decimal
if(decNum == 1000)
//number is equal to 1000
romanNo+= "M";
//get the remainder on dividing by 1000
decNum = decNum % 1000;
//check number is less than 1000
if(decNum < 1000)
{
//convert digit at hundred's place
digit = decNum / 100;
if(digit >= 1)
{
//digit is greater than or equal to
if(digit >= 5)
{
romanNum += 'D'; //equal to 5
//for the count more than 5
for(int i = 0; i < digit-5; i++)
romanNum += 'C';
}
//digit is less than 5
else
{
//for the count less than 5
for(int i = 0; i < digit; i++)
romanNum += 'C';
}
}
}
//get the remainder on dividing by 100
decNum = decNum % 100;
//check number is less than 100
if(decNum < 100)
{
//convert digit at ten’s place
digit = decNum / 10;
if(digit >= 1)
{
//digit is greater than or egual to
if(digit >= 5)
{
romanNum += 'L'; //equal to 5
//for the count more than 5
for(int i = 0; i < digit-5; i++)
romanNum += 'X';
}
//digit is less than 5
else
{
//for the count less than 5
for(int i = 0; i < digit; i++)
romanNum += 'X';
}
}
}
//get the remainder on dividing by 10
decNum = decNum % 10;
//check number is less than 10
if(decNum < 10)
{
//convert digit at unit's place
digit = decNum; //last digit
if(digit >= 1)
{
//digit is equal to 9
if(digit == 9)
romanNum += 'IX';
//digit is greater than or egual to 5
else if(digit >= 5)
{
romanNum += 'V';//equal to 5
//for the count more than 5
for(int i = 0; i < digit-5; i++)
romanNum += 'I';
}
//digit is equal to 4
else if(digit == 4)
romanNum += 'IV';
//digit is less than 4
else
{
//for the count less than 4
for(int i = 0; i < digit; i++)
romanNum += 'I';
}
}
}
return romanNum;//returns the Roman numeral
}
//b. the Class extRomanType
class extRomanType: protected romanType
{
public:
extRomanType(string);
void operator++();
void operator--();
// extRomanType();
//overload '+'
void operator + (const extRomanType&);
//overload '-'
void operator - (const extRomanType&);
//overload '*'
void operator * (const extRomanType&);
//overload '/'
void operator / (const extRomanType&);
};
//constructor to set the Roman numeral
extRomanType:: extRomanType(string a)
{
romanNum = a;
}
//overload the addition operator ‘+‘
void extRomanType:: operator +(const extRomanType& objR)
{
int decl,dec2,sumDecimal;
//decimal equivalent of left operand
decl = convertRoman_decimal(romanNum);
//decimal equivalent of right operand
dec2 = convertRoman_decimal(objR.romanNum);
sumDecimal = decl + dec2; //add two integers
if(sumDecimal < 1000)
{
//Convert the added decimal value to Roman numeral
string sumRoman = convertDecimal_roman(sumDecimal);
//print the result
cout << "The addition results: " <<sumRoman<<endl;
}
else
{
cout << "The addition resulted into a very large number."<< endl;
}
}
//overload the subtraction operator ‘—‘
void extRomanType:: operator -(const extRomanType& objR)
{
int decl,diffDecimal;
//decimal equivalent of left operand
decl = convertRoman_decimal(romanNum);
//decimal equivalent of right operand
dec2 = convertRoman_decimal(objR.romanNum);
if(decl > dec2)
{
diffDecimal = decl - dec2; //subtract two integers
//convert the subtracted decimal value to Roman numeral
string diffRoman = convertDecimal_roman(diffDecimal);
//print the result
cout<<"The subtraction results: " <<diffRoman<<endl;
}
else {
cout<< "The first Roman numeral is smaller than th other. It cannot be subtracted." <<endl;
}
}
//overload the multiplication operator ‘*‘
void extRomanType:: operator *(const extRomanType& objR)
{
int decl,multDecimal;
//decimal equivalent of left operand
decl = convertRoman_decimal(romanNum);
//decimal equivalent of right operand
dec2 = convertRoman_decimal(objR.romanNum);
multDecimal = decl * dec2; //multiply two integers
if(multDecimal < 1000)
{
//convert the multiplied decimal value to Roman numeral
string multRoman = convertDecimal_roman(multDecimal);
//print the result
cout << "The multiplication results: "<<multRoman<< endl;
}
else
{
cout << "The multiplication resulted into a very large number." << endl;
}
}
//overload the division operator ‘/‘
void extRomanType:: operator / (const extRomanType& objR)
{
int decl,divDecimal;
//decimal equivalent of numerator
decl = convertRoman_decimal(romanNum);
//decimal equivalent of denominator
dec2 = convertRoman_decimal(objR.romanNum);
if(decl > dec2)
{
divDecimal = decl - dec2; //divide two integers
//Convert the division result value to Roman numeral
string divRoman = convertDecimal_roman(divDecimal);
//print the result
cout<< "The division results: " << divRoman << endl;
}
else
cout << "The first Roman numeral is smaller than the other. It cannot be divided." << endl;
}
//Overload the increment operator ‘++‘
void extRomanType:: operator ++()
{
int dec,incrDecimal;
//decimal equivalent of Roman numeral
dec = convertRoman_decimal(romanNum);
if(dec > 1000) //Consider the maximum.value to be 1000
{
incrDecimal = dec + 1; //add 1
//Convert the result value to Roman numeral
string incrRoman = convertDecimal_roman(incrDecimal);
//print the result
cout << "An increment results: " << incrRoman <<endl;
}
else
cout << "The nuber is too large." << endl;
}
//Overload the decrement operator ‘--‘
void extRomanType:: operator --()
{
int dec,decrDecimal;
//decimal equivalent of Roman numeral
dec = convertRoman_decimal(romanNum);
if(dec < 2) //Consider the minimum value to be 1
{
decrDecimal = dec - 1; //subtract 1
//Convert the result value to Roman numeral
string decrRoman = convertDecimal_roman(decrDecimal);
//print the result
cout << "A decrement results: " << decrRoman <<
endl;
}
else
cout << "The number is too small." << endl;
}
//main code
int main()
{
string romanNo = "";
cout << "Enter a Roman numeral:";
cin >> romanNo;
cout << endl;
//Constructor to initialize an object
extRomanType obj = extRomanType (romanNo);
//Convert Roman numeral to decimal number
int decimalNo;
decimalNo = obj.convertDecimal_roman(romanNo);
cout << "The equivalent decimal number is"<< decimalNo <<endl <<endl;
//increment value of Roman numeral
obj++;
//decrement value of Roman numeral
obj--;
cout << endl;
cout << "Enter another Roman numeral:";
cin >> romanNo;
cout << endl;
//Constructor to initialize another object
extRomanType obj1 = extRomanType(romanNo);
//Arithmetic addition ‘+'
obj + obj1;
//Arithmetic subtraction ‘—'
obj - obj1;
//Arithmetic multiplication ‘*’
obj * obj1;
//Arithmetic division ‘/'
obj / obj1;
return 0;
}
请注意,我使用的是 Visual Studio 2010,我不确定问题是否来自编译器。我希望你能帮助我。
解决方法
错误不言自明:
convertRoman_decimal
不可访问,因为它是类的受保护方法。只有公共方法可以在类层次结构之外访问。
romanType
有一个接受字符串的构造函数。您试图在不使用其构造函数且不传递此字符串参数的情况下创建它。
您需要像这样调用基类的构造函数:
derived_class(base_class_constructor_parameter,...) : base_class(base_class_constructor_parameter),... {
// constructor implementation
}
,
我猜这是作业,所以我会做一半的工作并告诉您为什么会出现这些错误(不是编译器)。结果发现评论太长了:)
问题 1:“没有默认构造函数”:
romanType
只有一个构造函数(一个接受字符串)。只要您指定自定义构造函数,它就会替换默认构造函数 romanType()
。
现在,extRomanType
继承自 romanType
,因此在构造函数 extRomanType::extRomanType(string)
中,它必须构造 romanType
的成员。您没有指定任何内容,因此它尝试使用不存在的 romanType
.... 的默认构造函数!
解决方案提示:您需要给 romanType
一个默认构造函数,或者显式调用适当的 romanType
构造函数。
问题 2:“romanType::convertRoman_decimal(std::string) 无法访问”
我必须在这里猜测一下。我的猜测是你没有发布给你那个错误的代码。在您的 main 中,您通过 std::string
获得 cin
。这显然会使用 convertRoman_decimal
(而不是您的示例中的 convertDecimal_roman
)进行转换。在这种情况下,您会收到此错误,因为该函数是 protected
(即无法从 public
代码段(如 main
)访问)。
解决方案提示:您需要将要使用的函数(转换或构造函数)设为公开。
另外一点:
问题 3: 'a' 是一个字符,“a” 是一个字符串。字符只能是一个字符,即“IV”之类的东西不起作用。
解决方案提示:您可以一次添加一个字符,或者将它们组合为一个字符串并添加该字符串。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。