首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类构造函数中的数据结构错误和C++中类的不可访问性

类构造函数中的数据结构错误和C++中类的不可访问性
EN

Stack Overflow用户
提问于 2021-02-19 05:34:35
回答 2查看 28关注 0票数 0

我的代码中有一些问题,比如:"romanType“类没有默认的构造函数,romanType::convertRoman_decimal(std::string)是不可访问的。我做了我能做的但我做不到。

下面是完整的代码:

代码语言:javascript
复制
#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, dec2, 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, dec2, 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, dec2, 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,我不确定问题是否来自编译器。我希望你能帮助我。

EN

回答 2

Stack Overflow用户

发布于 2021-02-19 05:52:30

错误是不言而喻的:

convertRoman_decimal不可访问,因为它是该类的受保护方法。只有公共方法才能在类层次结构外部访问。

romanType有一个接受字符串的构造函数。您正尝试在不使用其构造函数和不传递此字符串参数的情况下创建它。

您需要像这样调用基类的构造函数:

代码语言:javascript
复制
derived_class(base_class_constructor_parameter, ...) : base_class(base_class_constructor_parameter), ... {
   // constructor implementation
}
票数 0
EN

Stack Overflow用户

发布于 2021-02-19 05:55:18

我猜这是家庭作业,所以我会做一半的工作,并告诉你为什么会出现这些错误(不是编译器)。这对于一个评论来说是一个太长的响应:)。

问题1:“没有默认构造函数”:

romanType只有一个构造函数(一个接受字符串)。一旦指定了自定义构造函数,它就会替换默认的构造函数romanType()

现在,extRomanType继承自romanType,因此在ctor extRomanType::extRomanType(string)中,它必须构造romanType的成员。您没有指定任何内容,因此它会尝试使用默认的构造函数romanType...这是不存在的!

解决方案提示:您需要为romanType提供一个默认构造函数,或者显式地调用适当的romanType构造函数。

问题2:"romanType::convertRoman_decimal(std::string)无法访问“

我不得不在这里猜测一下。我的猜测是你没有发布给你那个错误的代码。在你的main中,你通过cin得到一个std::string。这显然是使用convertRoman_decimal (而不是您示例中的convertDecimal_roman )进行转换的。在这种情况下,您会得到这个错误,因为函数是protected的(即不能从像main这样的public代码段访问)。

解决方案提示:您需要公开要使用的函数(转换或构造函数)。

还有一点要提出来:

问题3:'a‘是一个字符,"a“是一个字符串。字符只能是一个字符,即像“IV”这样的字符将不起作用。

解决方案提示:您可以一次添加一个字符,也可以将它们组合为一个字符串并添加该字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66268481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档