餵我收到編譯錯誤,我不明白爲什麼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; }
智能感知錯誤不是編譯器錯誤。發佈實際的編譯器錯誤(以字母「C」開頭的錯誤)。 – PaulMcKenzie
一個好的策略是刪除代碼,直到錯誤停止,然後慢慢開始再次添加代碼。 – TankorSmash
請不要只是將所有代碼轉儲到問題中,而是發佈[mcve]。 –