Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的类+;:无法将两个旧函数集成到一个";“类格式”;_C++_Function_Class_Object - Fatal编程技术网

C++ C+中的类+;:无法将两个旧函数集成到一个";“类格式”;

C++ C+中的类+;:无法将两个旧函数集成到一个";“类格式”;,c++,function,class,object,C++,Function,Class,Object,更新:我有一个作业,我应该根据键盘上键入的内容打印字母分布。打印应为示例A:5 B:8 C:0。。。等等,这是由函数berakna_histogram_abs和skriv_histogram_abs完成的。从第一次提到的功能中还应打印出字母总数。现在看来,它适用于每个字母的金额正确的情况,但不是总金额始终为0 我的第一个问题是:当字母总数始终为0时,我可以在代码中的哪里找到问题 #include <string> #include <cctype> #include &l

更新:我有一个作业,我应该根据键盘上键入的内容打印字母分布。打印应为示例A:5 B:8 C:0。。。等等,这是由函数berakna_histogram_abs和skriv_histogram_abs完成的。从第一次提到的功能中还应打印出字母总数。现在看来,它适用于每个字母的金额正确的情况,但不是总金额始终为0

我的第一个问题是:当字母总数始终为0时,我可以在代码中的哪里找到问题

#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

// Global constants:
const int ANTAL_BOKSTAVER = 26;  //A-Z
const int ANTAL_SPRAK = 4;
const double TOLK_HJALP[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
       {{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77, //English
         7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
         0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
         1.89,0.03},
        {7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75, //French
         7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
         1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
         0.30,0.15},
        {9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94,  //Swedish
         3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
         0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
         0.45,0.00},
        {5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79, //German
         8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
         0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
         0.69,1.24}};


// Class declaration
class Text
{
private:
    string text;
     bool histOK;
     int f[ANTAL_BOKSTAVER];
public:
Text ();
Text (string nytext, bool nyhistOK,int nyf[] );
 string get_text();
 bool get_histOK();
 int get_f();
 void setText(string nyText);
 bool beraknaHistogramAbs(const string &str,int hist[]);
 void skrivHistogramAbs(string Alfab, int hist[]);
    
};

//Funktiondeklarations
bool beraknaHistogramAbs(const string &str,int hist[]);
void skriv_histogram_abs(string Alfab, int hist[]);

// Main program:
int main()
{
string text, alfabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool histOK = true;
int f[ANTAL_BOKSTAVER];

Text minText; // skapar ett objekt
  
cout <<"Ge en rad med text:" << endl;
getline(cin,text);
 
cout << "\nResultat för bokstäverna A-Z" << endl;
cout << "Totala antalet bokstäver: " << true << endl;
    


  // Calling constructor
  minText.setText( text );
 histOK = minText.beraknaHistogramAbs(text,f);
  minText.skrivHistogramAbs(alfabet,f );

  return 0;
}
//Class implementation

Text::Text()
{
    text="";
    histOK=true;
    
}

Text::Text (string nytext, bool nyhistOK,int nyf[])
{
    text=nytext;
    histOK=nyhistOK;
        }

string Text::get_text()
{
    return text;
}

bool Text::get_histOK()
{
    return histOK;
}



bool Text::beraknaHistogramAbs(const string &str,int hist[])
{
    int index, totA=0;
    char b;
    for (int i=0; i < ANTAL_BOKSTAVER; i++) {
    hist[i] = 0;
      }
     
     for (int i = 0; i <(int)str.length();i++)
     {
         b = toupper(str.at(i));
         index = b - 'A';
          
         if (isalpha(b))
         { hist[index] +=1;
              totA+=1;
         }
     }
    
    return totA;
}
    bool letters (int &totA) // A bool variable is returned that is true if there contains any letters, else false.
    {
        bool status=true;
        if(totA==0){
        status=false;
      }
        return true;
}

void Text::skrivHistogramAbs(string Alfab, int hist[])
{  int i = 0;
  
    cout << "\nLetter Distribution: " << endl;
    cout << "Letters" << "\t" << "Amount" << endl;

    
    while (i < ANTAL_BOKSTAVER)
     {
       cout << Alfab[i] << ":\t" << hist[i] << endl; i++;
     }
}

void Text::setText(string nyText)
{
    text=nyText;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//全局常数:
常数int ANTAL_BOKSTAVER=26//A-Z
安塔尔斯帕克常数=4;
双隧道施工图[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
{{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77,//英语
7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
1.89,0.03},
{7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75,//法语
7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
0.30,0.15},
{9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94,//瑞典语
3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
0.45,0.00},
{5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79,//德语
8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
0.69,1.24}};
//类声明
课文
{
私人:
字符串文本;
布尔-希斯塔克;
int f[ANTAL_BOKSTAVER];
公众:
文本();
文本(字符串nytext,bool nyhistOK,int nyf[]);
字符串get_text();
bool get_histOK();
int get_f();
void setText(字符串nyText);
bool-beraknaHistogramAbs(常量字符串和字符串,int-hist[]);
void-skrivHistogramAbs(字符串Alfab,int-hist[]);
};
//FunktionDeklaations
bool-beraknaHistogramAbs(常量字符串和字符串,int-hist[]);
void skriv_histogram_abs(字符串Alfab,int hist[]);
//主程序:
int main()
{
字符串文本,alfabet=“abcdefghijklmnopqrstuvxyz”;
bool histOK=真;
int f[ANTAL_BOKSTAVER];
Text minText;//skapar ett objekt

我有一个简单的建议,把函数声明放在
main()
之前

因此,您的代码需要进行更改:

#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

// global constants:

const int ANTAL_BOKSTAVER = 26;  //A-Z
const int ANTAL_SPRAK = 4;
const double TOLK_HJALP[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
       {{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77, //engelska
         7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
         0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
         1.89,0.03},
        {7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75, //franska
         7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
         1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
         0.30,0.15},
        {9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94,  //svenska
         3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
         0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
         0.45,0.00},
        {5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79, //tyska
         8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
         0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
         0.69,1.24}};


//  Klassdeklarationen
class Text
{
private:
    string text;
     bool histOK;
     int f[ANTAL_BOKSTAVER];
public:
 // konstruktorer
    Text ();
    Text (string nytext, bool nyhistOK,int ny[ANTAL_BOKSTAVER] );
// selektorer
 string Gettext;
 bool GethistOK;
    int Getf;
//klassmetoder
 void setText(string nyText);
 bool beraknaHistogramAbs();
 void skrivHistogramAbs();
    
};

// NOTICE: here is function declaration
void skriv_histogram_abs(string Alfab, int hist[]);

// Main program:

int main()
{

// Deklarera variabler
  string text;
  bool histOK;
//Object
  Text minText;
  <<"Ge en rad med text:" << endl;
  getline(cin,text);

  
  minText.setText( text );
  histOK = minText.beraknaHistogramAbs( );
  minText.skrivHistogramAbs( );

  return 0;
}
//--------------------------------------------------------
// Class implementation
Text::Text()
{
    // not defined yet
}

Text::Text (string nytext, bool nyhistOK,int ny[ANTAL_BOKSTAVER])
{
    //not defined yet
}

bool Text::beraknaHistogramAbs()
{ bool a = true;
    if(histOK!=0)
    { a=false;
    }
  
   return histOK;
}
void Text::skrivHistogramAbs()
{
    cout << "\nBokstavsfördelning: " << endl;
    cout << "Bokst." << "\t" << "Antal" << endl;
}
void Text::setText(string nyText)
{

}
//--------------------------------------------------------
// Function definitions (my old functions where I'm not sure if they should be redefined under class implementation)

int berakna_histogram_abs(const string &str, int hist[])
{
        int index, totA=0;
        char b;
        
    for (int i=0; i < ANTAL_BOKSTAVER; i++) { // Nollställer histogram
       hist[i] = 0;
         }
        
        for (int i = 0; i <(int)str.length();i++)
        {
            b = toupper(str.at(i));
            index = b - 'A';
             
            if (isalpha(b))
            { hist[index] +=1;
                 totA+=1;
            }
       }
            return totA;
}

void skriv_histogram_abs(string Alfab, int hist[])
{
    
       int i = 0;
    cout << "\nBokstavsfördelning: " << endl;
    cout << "Bokst." << "\t" << "Antal" << endl;
  
           while (i < ANTAL_BOKSTAVER)
           {
               cout << Alfab[i] << ":\t" << hist[i] << endl; i++;
            }
              
      }
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//全局常数:
常数int ANTAL_BOKSTAVER=26;//A-Z
安塔尔斯帕克常数=4;
双隧道施工图[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
{{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77,//engelska
7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
1.89,0.03},
{7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75,//franska
7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
0.30,0.15},
{9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94,//svenska
3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
0.45,0.00},
{5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79,//tyska
8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
0.69,1.24}};
//克拉斯德克拉科宁
课文
{
私人:
字符串文本;
布尔-希斯塔克;
int f[ANTAL_BOKSTAVER];
公众:
//康斯特鲁克托雷
文本();
文本(字符串nytext,bool nyhistOK,int-ny[ANTAL_-BOKSTAVER]);
//塞莱克托勒
字符串Gettext;
布尔·盖茨托克;
int-Getf;
//克拉斯梅托德
void setText(字符串nyText);
bool beraknaHistogramAbs();
void skrivHistogramAbs();
};
//注意:这里是函数声明
void skriv_histogram_abs(字符串Alfab,int hist[]);
//主程序:
int main()
{
//德克拉雷拉变量器
字符串文本;
布尔-希斯塔克;
//反对
文本minText;

我有一个快速建议,将函数声明放在
main()
之前

因此,您的代码需要进行更改:

#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

// global constants:

const int ANTAL_BOKSTAVER = 26;  //A-Z
const int ANTAL_SPRAK = 4;
const double TOLK_HJALP[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
       {{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77, //engelska
         7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
         0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
         1.89,0.03},
        {7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75, //franska
         7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
         1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
         0.30,0.15},
        {9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94,  //svenska
         3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
         0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
         0.45,0.00},
        {5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79, //tyska
         8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
         0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
         0.69,1.24}};


//  Klassdeklarationen
class Text
{
private:
    string text;
     bool histOK;
     int f[ANTAL_BOKSTAVER];
public:
 // konstruktorer
    Text ();
    Text (string nytext, bool nyhistOK,int ny[ANTAL_BOKSTAVER] );
// selektorer
 string Gettext;
 bool GethistOK;
    int Getf;
//klassmetoder
 void setText(string nyText);
 bool beraknaHistogramAbs();
 void skrivHistogramAbs();
    
};

// NOTICE: here is function declaration
void skriv_histogram_abs(string Alfab, int hist[]);

// Main program:

int main()
{

// Deklarera variabler
  string text;
  bool histOK;
//Object
  Text minText;
  <<"Ge en rad med text:" << endl;
  getline(cin,text);

  
  minText.setText( text );
  histOK = minText.beraknaHistogramAbs( );
  minText.skrivHistogramAbs( );

  return 0;
}
//--------------------------------------------------------
// Class implementation
Text::Text()
{
    // not defined yet
}

Text::Text (string nytext, bool nyhistOK,int ny[ANTAL_BOKSTAVER])
{
    //not defined yet
}

bool Text::beraknaHistogramAbs()
{ bool a = true;
    if(histOK!=0)
    { a=false;
    }
  
   return histOK;
}
void Text::skrivHistogramAbs()
{
    cout << "\nBokstavsfördelning: " << endl;
    cout << "Bokst." << "\t" << "Antal" << endl;
}
void Text::setText(string nyText)
{

}
//--------------------------------------------------------
// Function definitions (my old functions where I'm not sure if they should be redefined under class implementation)

int berakna_histogram_abs(const string &str, int hist[])
{
        int index, totA=0;
        char b;
        
    for (int i=0; i < ANTAL_BOKSTAVER; i++) { // Nollställer histogram
       hist[i] = 0;
         }
        
        for (int i = 0; i <(int)str.length();i++)
        {
            b = toupper(str.at(i));
            index = b - 'A';
             
            if (isalpha(b))
            { hist[index] +=1;
                 totA+=1;
            }
       }
            return totA;
}

void skriv_histogram_abs(string Alfab, int hist[])
{
    
       int i = 0;
    cout << "\nBokstavsfördelning: " << endl;
    cout << "Bokst." << "\t" << "Antal" << endl;
  
           while (i < ANTAL_BOKSTAVER)
           {
               cout << Alfab[i] << ":\t" << hist[i] << endl; i++;
            }
              
      }
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//全局常数:
常数int ANTAL_BOKSTAVER=26;//A-Z
安塔尔斯帕克常数=4;
双隧道施工图[ANTAL_SPRAK][ANTAL_BOKSTAVER]=
{{8.27,1.48,2.94,4.03,11.78,2.22,1.72,6.77,//engelska
7.39,0.12,0.81,3.76,2.85,6.71,7.79,1.54,
0.05,5.95,6.69,9.07,2.66,1.13,2.14,0.19,
1.89,0.03},
{7.97,1.40,3.55,3.79,16.89,1.02,1.00,0.75,//franska
7.08,0.38,0.04,5.51,2.82,8.11,5.19,2.78,
1.01,6.69,8.35,7.22,6.09,1.35,0.02,0.54,
0.30,0.15},
{9.50,1.11,1.53,5.30,8.94,1.74,3.57,3.94,//svenska
3.98,0.89,3.26,4.93,3.41,8.46,5.01,1.77,
0.00,6.73,5.56,9.20,1.94,2.42,0.00,0.05,
0.45,0.00},
{5.12,1.95,3.57,5.07,16.87,1.35,3.00,5.79,//tyska
8.63,0.19,1.14,3.68,3.12,10.64,1.74,0.42,
0.01,6.30,6.99,5.19,3.92,0.77,1.79,0.01,
0.69,1.24}};
//克拉斯德克拉科宁
课文
{
私人:
字符串文本;
布尔-希斯塔克;
int f[ANTAL_BOKSTAVER];
公众:
//康斯特鲁克托雷
文本();
文本(字符串nytext,bool nyhistOK,int-ny[ANTAL_-BOKSTAVER]);
//塞莱克托勒
字符串Gettext;
布尔·盖茨托克;
int-Getf;
//克拉斯梅托德
void setText(字符串nyText);
bool beraknaHistogramAbs();
void skrivHistogramAbs();
};
//注意:这里是函数声明
void skriv_histogram_abs(字符串Alfab,int hist[]);
//主程序:
int main()
{
//德克拉雷拉变量器
字符串文本;
布尔-希斯塔克;
//反对
文本minText;

我不认为有类有什么意义。在
main()
函数中使用
std::map
std::getline
std::string
。或者可以使用256个插槽的数组来代替
std::map
。将字符用作数组的索引:
int counts[256]={0};/**