C ++编译问题

时间:2016-03-03 00:33:13

标签: c++ data-structures

您好我收到了编译错误,我不明白为什么

  • 2智能感知:预期标识符为c:\ Users \ Sean \ Documents \ Visual Studio 2013 \ Projects \ mystring \ mystring \ mystring.cpp 93 7 mystring

  • 3智能感知:期待a&#39 ;;' c:\ Users \ Sean \ Documents \ Visual Studio 2013 \ Projects \ mystring \ mystring \ mystring.cpp 93 10 mystring

  • 4智能感知:成员函数" main_savitch_4 :: mystring :: operator ="可能不会在其类c:\ Users \ Sean \ Documents \ Visual Studio 2013 \ Projects \ mystring \ mystring \ mystring.cpp之外重新声明106 17 mystring

  • 5智能感知:期待a&#39 ;;' c:\ Users \ Sean \ Documents \ Visual Studio 2013 \ Projects \ mystring \ mystring \ mystring.cpp 107 2 mystring

  • 6智能感知:多个运营商">>"匹配这些操作数:         function" operator>>(std :: istream& ins,main_savitch_4 :: mystring& target)"         function" main_savitch_4 :: operator>>(std :: istream& ins,main_savitch_4 :: mystring& target)"         操作数类型是:std :: istream>> main_savitch_4 :: mystring c:\ Users \ Sean \ Documents \ Visual Studio 2013 \ Projects \ mystring \ mystring \ mystring.cpp 246 7 mystring

    #include <iostream>
    #include "mystring.h"
    #include <cstring>
    using namespace std;
    using namespace main_savitch_4;
    
    
    //Constructors- sets initial values for memeber variables
    
      mystring::mystring(const char str[] = "")
    
    //returns a pointer referencing [0] in an array
    {
    current_length = 0;
    int pt = 0;
    
    if (str[pt] != '\0') //this increases the array size to the string size
    {
        ++current_length;
        ++pt;
    }
    
    sequence = new char[current_length + 1];
    while (current_length >= pt)
    {
        sequence[pt] = str[pt];
        ++pt;
    }
    allocated = current_length + 1;
    }
    
    mystring::mystring(const mystring& source)
    {
    sequence = new char[source.allocated];
    current_length = source.current_length; //returns mystring object with same
    allocated = source.allocated;           //length and allocation as source
    for (int i = 0; i <= allocated; ++i)
    {
        sequence[i] = source.sequence[i]; //copies characters to new object
    }
    
    }
    
    //Destructor- frees space
    mystring::~mystring()
    {
            delete[] sequence;
    }
    
    void mystring::operator +=(const mystring& addend)//adds another string ontop of the original
    {
    
    reserve(current_length + addend.current_length + 1);
    strcpy(sequence + current_length, addend.sequence);
    current_length += addend.current_length;
    }
        void mystring::operator +=(const char addend[])
        {
    int leng = current_length + strlen(addend);
    if (leng >= allocated)
            {
        reserve(leng + 1);
    }
    
    strcpy(sequence + current_length, addend);
    current_length = leng;
    }
    
    void mystring::operator +=(char addend)
    {
    while (current_length >= allocated)
    {
        reserve(current_length + 1);
    }
    
    sequence[current_length] = addend;
    sequence[current_length + 1] = '\0'; //increases size to allow for \0     character
    ++current_length;
    
            }   
    
     void mystring::reserve(size_t n) //size_t represents the size of object
    // n is the size
    
    
    {
    
        if (allocated < n)
        {
    
        char[] sequence = new char[n];
        for (int i = 0; i <= current_length; ++i)
        {
            sequence + i;
            allocated = n;
        }
    
    }
    
    
    /////////////////////
    
    
    void mystring::operator =(const mystring& source)
    {
    
        delete[] sequence;
        sequence = new char[source.allocated];
        allocated = source.allocated;
        current_length = source.current_length;
    
        for (int idx = 0; idx <= source.allocated; ++idx)
        {
            sequence[idx] = source.sequence[idx];
        }
    
    }
    
    
    
    char mystring::operator [ ](size_t position) const
    
    {
        return sequence[position];
    }
    
    
    std::ostream& operator <<(std::ostream& outs, const mystring& source)
    
    {
        outs << source.sequence;
    
        return outs;
    }
    
    bool operator ==(const mystring& s1, const mystring& s2)
    
    {
        int length = 0;
        bool gate = false;
    
    
        if (s1.length() >= s2.length())
            length = s1.length();
        else
            length = s2.length();
    
        for (int idx = 0; idx < length; ++idx)
        {
            if (s1[idx] - s2[idx] == 0 && idx == length - 1)
                gate = true;
        }
    
        return gate;
    }
    
    bool operator !=(const mystring& s1, const mystring& s2)
    
    {
        return !(s1 == s2);
    }
    
    bool operator >=(const mystring& s1, const mystring& s2)
    
    {
        if (s1 < s2)
            return false;
        else
            return true;
    }
    
    bool operator <=(const mystring& s1, const mystring& s2)
    
    {
        if (s1 > s2)
            return false;
        else
            return true;
    }
    
    bool operator > (const mystring& s1, const mystring& s2)
    
    {
        bool gate = false;
        int length = 0;
    
        if (s1.length() <= s2.length())
            length = s1.length();
        else if (s1.length() > s2.length())
            length = s2.length();
    
        for (int idx = 0; idx < length; ++idx)
        {
            if (s1[idx] - s2[idx] < 0)
            {
                gate = true;
                idx = length;
            }
            else if (idx == length - 1 && s1.length() < s2.length())
            {
                gate = true;
            }
        }
    
        return gate;
    }
    
    bool operator < (const mystring& s1, const mystring& s2)
    
    {
        if (s1 != s2 && s2 > s1)
            return true;
        return false;
    }
    
    
    mystring operator +(const mystring& s1, const mystring& s2)
    
    {
        mystring temp(s1);
        temp += s2;
        return temp;
    }
    
    std::istream& operator >>(std::istream& ins, mystring& target)
    
    {
        while (ins && isspace(ins.peek()))
        {
            ins.ignore();
        }
    
        target = ""; //Clear out any junk in the target
    
        while (ins.peek() != '\n')
            target += ins.get();
    
        return ins;
    }
    
    std::istream& getline(std::istream& ins, mystring& target)
    
    {
        ins >> target;
    
        return ins;
    }
    

1 个答案:

答案 0 :(得分:1)

函数保留中的if语句:

void mystring::reserve(size_t n)
{
    if (allocated < n)
    {

    char[] sequence = new char[n];
    for (int i = 0; i <= current_length; ++i)
    {
        sequence + i;
        allocated = n;
    }
}

似乎没有关闭。尝试添加}。