例外のスローを許可するために与えられたコードを変更しています。問題は、宣言が例外タイプの Error を有効なクラスとして受け入れていないように見えることです。
コードは以下のとおりです。
// 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: error: expected ;' 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>