0

我正在修改给我的代码以允许抛出异常。问题是声明似乎不接受错误,将异常类型作为有效类。

代码如下:

// Interface for a simple String class to encapsulate a C character string

#ifndef _MYSTRING_H_
#define _MYSTRING_H_

#include <string.h>
#include <iostream>
#include <stdexcept>
using namespace std;

class MyString {
public:
    // Constructors and destructor
    MyString(const char * = ""); // Create from C string
    MyString(const MyString &); // Copy constructor
    ~MyString() { delete [] sdata; }

    // Assignment
    MyString & operator = (const MyString &);
    MyString & operator = (const char *);

    MyString & operator += (const MyString &);
    MyString & operator += (const char &);

    // Character access
    char & operator [] (int i) throw(MyString::Error);
    char operator [] (int i) const throw(MyString::Error){
        return (i < 0 || i >= len) ? '\0' : sdata[i];
    }

    // Substrings
    MyString operator () (unsigned int start, unsigned int count) const throw(MyString::Error);

    // Concatenation
    MyString operator + (const MyString &) const;
    MyString operator + (const char &) const;

    // Cast to c string
    operator const char * () const { return sdata; }

    // Query methods
    unsigned int length() const { return len; }

    class Error: public exception{
        public:
            //ERROR CODES
            static const int SUBSTRING_ERR = 0;
            static const int INDEX_ERR  = 1;
            static const int ALLOC_ERR  = 2;

            int errorCode;
            int leftIndex;
            int count;
            int size;

            Error(int errCode){ //Alloc error
                errorCode = errCode;
            }
            Error(int errCode, int left, int len){ //Index error
                errorCode = errCode;
                leftIndex   = left;
                size        = len;
            }
            Error(int errCode, int left,int substrCount, int len){ //Substring error
                errorCode = errCode;
                leftIndex   = left;
                size        = len;
                count = substrCount;
            }

    };

private:
    char * sdata;           // Storage for the characters
    unsigned int len;       // Current length

    // Private constructor for pre-allocation
    MyString(const char *, unsigned int);  
};

inline ostream & operator << (ostream & o, const MyString & s) {
    return o << (const char *) s;
}

inline bool operator == (const MyString  & lhs, const MyString & rhs) {
    return (::strcmp(lhs, rhs) == 0) ? true : false;
}

inline bool operator != (const MyString  & lhs, const MyString & rhs) {
    return (lhs == rhs) ? false : true;
}

inline bool operator < (const MyString  & lhs, const MyString & rhs) {
    return (::strcmp(lhs, rhs) < 0) ? true : false;
}

inline bool operator >= (const MyString  & lhs, const MyString & rhs) {
    return (lhs < rhs) ? false : true;
}

inline bool operator > (const MyString  & lhs, const MyString & rhs) {
    return (::strcmp(lhs, rhs) > 0) ? true : false;
}

inline bool operator <= (const MyString  & lhs, const MyString & rhs) {
    return (lhs > rhs) ? false : true;
}

inline ostream & operator << (ostream & o, const MyString::Error & s) {
    return o << "NOM"; //TODO
}

#endif

这是完整的错误消息:

/Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:26:错误:预期类型说明符/Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:26:错误:预期)' /Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:26: error: expected ‘;’ /Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:27: error: expected type-specifier /Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:27: error: expected)'/Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:27:错误:预期';' /Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:32:错误:预期;' before ‘MyString’ /Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:32: error: expected type-specifier /Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:32: error: expected)'/Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330 /6/Mystring.h:32: 错误:预期';'</p>

4

2 回答 2

2

您需要在MyString::Error使用该类之前声明它:

class MyString {
 public:
  // constructors, assignment operators, increment operators as before

  class Error: public std::exception{ ... }

  // Character access and methods using MyString::Error
  char & operator [] (int i) throw(MyString::Error);
  char operator [] (int i) const throw(MyString::Error){ ... }

};

我强烈建议不要using namespace std在头文件中使用任何其他名称空间。此外,在 C++11 中不鼓励和弃用异常规范。

于 2012-06-08T06:12:54.883 回答
0

我相信它是operator[]- 单个令牌,而不是operator []- 两个令牌。其他地方也一样。

于 2012-06-08T06:12:47.160 回答