2016-03-03 174 views
-1

喂我收到编译错误,我不明白为什么C++编译问题

  • 2智能感知:预期的标识符C:\用户\肖恩\文档\ Visual Studio的2013 \项目\ MyString的\的MyString \ mystring.cpp 93 7 mystring

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

  • 4 IntelliSense:成员函数“main_savitch_4 :: mystring :: operator =”不能在外部重新声明其类c:\ Users \ Sean \ Documents \ Visual Studio 2013 \ Projects \ mystring \ mystring \ mystring.cpp 106 17 mystring

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

  • 6 IntelliSense:多个操作符“>>”匹配这些操作数: function“operator> >(标准:: istream的&插件,main_savitch_4 :: MyString的&目标)” 函数 “main_savitch_4 ::运算>>(标准:: istream的&插件,main_savitch_4 :: MyString的&目标)” 操作数的类型有:标准:: 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; 
    } 
    
+0

智能感知错误不是编译器错误。发布实际的编译器错误(以字母“C”开头的错误)。 – PaulMcKenzie

+0

一个好的策略是删除代码,直到错误停止,然后慢慢开始再次添加代码。 – TankorSmash

+0

请不要只是将所有代码转储到问题中,而是发布[mcve]。 –

回答

1

这如果储备功能的语句:

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; 
    } 
} 

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

+0

'char [] sequence = new char [n];'这是否有效的C++? – PaulMcKenzie