Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:23:35+00:00 2026-06-05T09:23:35+00:00

I am modifying code that was given to me to allow the throwing of

  • 0

I am modifying code that was given to me to allow the throwing of exceptions. The problem is that the declarations don’t seem to be accepting Error, the exception type as a valid class.

The code is below:

// 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

Here is the full error message:

/Users/alexanderstein/Documents/School/2011-2012/Term 3/CIS 330/6/Mystring.h:26: error: expected type-specifier
/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: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: error: expected ‘;’
/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: error: expected ‘;’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:23:36+00:00Added an answer on June 5, 2026 at 9:23 am

    You need to declare the MyString::Error class before it is used:

    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){ ... }
    
    };
    

    I would strongly advise against using namespace std and any other namespace in header files. Also, exception specifications are discouraged and deprecated in C++11.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm modifying some code that calls enumerate on a list declared via a list
I've been writing code that processes certain fields of an object by modifying their
Have you ever created or encountered a self modifying code in Java? If yes,
I am modifying this code: https://github.com/jasondavies/d3-cloud <script> d3.layout.cloud().size([300, 300]) .words([ Hello, world, normally, you,
I've been modifying some code to work between Mac OS X and iPhone OS.
I am struggling with modifying some code in Customer registration management module. Instant checkout
I'm new to C# and directly diving into modifying some code for a project
I'm working on modifying some existing code for a payment gateway and I'm not
I am using apple's custom table view cell code and modifying the drawRect code
I'm stuck modifying someone else's source code, and unfortunately it's very strongly NOT documented.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.