C ++代码错误,我需要帮助解决它

时间:2016-09-25 23:04:37

标签: c++

所以我需要一些帮助来了解如何在终端中修复此错误:

String.cpp: In constructor ‘String::String()’:
String.cpp:16:1: error: uninitialized const member in ‘const int’ [-fpermissive]
 String::String()
 ^
In file included from String.cpp:12:0:
String.h:16:13: note: ‘const int String::max_length’ should be initialized
   const int max_length;
             ^
String.cpp: In constructor ‘String::String(char*)’:
String.cpp:21:1: error: uninitialized const member in ‘const int’ [-fpermissive]
 String::String(char str[])
 ^
In file included from String.cpp:12:0:
String.h:16:13: note: ‘const int String::max_length’ should be initialized
   const int max_length;

这是String.cpp类,我必须使用名为String.h的头类中的原始数据类型来实现我自己的字符串类。

String.cpp:
#include <iostream>
#include <cstdlib>
#include <string>
#include "String.h"

using namespace std;

String::String()
{
    _length = 0;
}

String::String(char str[])
{
    int i = 0;
    _length = 0;
        while(str[i] != '\0')
        {
            data[i] = str[i];
            i++;
            _length++;
        }
    data[i] = '\0';
}

void String::print()
{
    if(_length == 0)
        cout<<"NULL"<<endl;
        for(int i = 0; i < _length; i++)
            cout<<data[i];
            cout<<endl;
}

int String::length()
{
    return _length;
}

void String::clear()
{
    _length = 0;
}

bool String::empty()
{
    if(_length == 0)
        return true;
    return false;
}

char String::at(int index)
{
    return data[index];
}

int String::find(char substr[], int startIndex)
{
    int subLen = 0;
    while(substr[subLen] != '\0')
        subLen++;
        for(int i = 0; i < _length - subLen; i++)
        {
            bool match = true;
            for(int j = 0; j < subLen; j++)
                if(data[i+j] != substr[j])
                {
                    match = false;
                    break;
                }
                if(match)
                    return i;
        }
    return -1;   
}

bool String::equal(String &two)
{
    if(_length != two.length())
        return false;
        for(int i = 0; i < _length; i++)
            if(data[i] != two.at(i))
            return false;
        return true;   
}

bool String::add(String &two)
{
    for(int i = 0; i < two.length(); i++)
        data[_length++] = two.at(i);
        data[_length] = '\0';
    return true;
}


String.h: 
using namespace std;

class String 
{
    private:

        const int max_length;
        int _length;
        char data[256];
        int getCharArraySize(char arr[]);

    public:

        String();
        String(char str[]);

        void print();
        int length();
        void clear(); 
        bool empty();
        char at(int index); 
        int find(char substr[], int startIndex);
        bool equal(String &two);
        bool add(String &two);


};

1 个答案:

答案 0 :(得分:1)

 const int max_length;

您已将此声明为const,因此您需要对其进行初始化。

 const int max_length = 128781728178278172;

ETA:是的,XD值太高了

然而,这不能很好地编译,这是一种更好的方法。

String::String() : max_length(256)
{
  _length = 0;
}

String::String(char str[]) : max_length(256)
{

还有另一个错误,缺少此声明:

bool equal(String&amp; two);