i can't fix my errors. please help me out...
here's the ITEMTYPE part of my code:

//itemtype.h:::::
#ifndef ITEMTYPE_H_INCLUDED
#define ITEMTYPE_H_INCLUDED

class itemtype

public:

    itemtype();
    void print()const;
    void initialize(int number);

private:

    int value;
};

#endif



//itemtype.cpp::::::
#include "itemtype.h"
#include <iostream>
using namespace std;

itemtype::Itemtype()   // this is the line where the errors are shown
{
    value=0;
}

void itemtype::initialize(int number)
{
    value=number;
}

void itemtype::print()const
{
    cout<<value<<" ";
}

ERRORS:::::::

line 27: error: ISO C++ forbids declaration of 'Itemtype' with no type [-fpermissive]
line 27:error: no 'int itemtype::Itemtype()' member function declared in class 'itemtype'

Recommended Answers

All 11 Replies

Sorry. the line number is 27!!!

line 16 }; where is { opening brace

Try a code highlighting editor, I get a lot of use from notepad++, whenever the cursor hits a brace, bracket < > or other enclosure it highlights the oposite one, or goes red if there is no opposite one

It should be itemtype::itemtype() (case sensitive)

I put that opening brace in my code. don't know how that's missing here! anyway, if i use "itemtype::Itemtype()" instead of "itemtype::Itemtype()", then the error says that,"multiple definition of itemtype::Itemtype()" and there occurs lot of other errors.

sorry that should be...If i use itemtype::itemtype() instead ofitemtype::Itemtype() then the error says that,"multiple definition of itemtype::itemtype() and there occurs lot of other errors.

should i post my whole code?

It shouldn't be necessary to do so, but it might help put the class in context. Posting the error log would probably be more helpful, in this case, especially since I was able to compile the code (with the open brace restored and the c'tor name corrected) with no trouble (using GCC). I did get a warning that recommended that you move the initialization of value to the initalization list, which I have done here:

//itemtype.h:::::
#ifndef ITEMTYPE_H_INCLUDED
#define ITEMTYPE_H_INCLUDED

class itemtype
{
public:
    itemtype(): value(0) {return;};
    void print()const;
    void initialize(int number);

private:
    int value;
};

#endif

and

//itemtype.cpp::::::
#include "itemtype.h"
#include <iostream>

void itemtype::initialize(int number)
{
    value=number;
}

void itemtype::print()const
{
    std::cout<<value<<" ";
}

But even that is just a recommendation, not an error.

BTW, as an aside, it is better to avoid using directives in class definition and implementation files, and use explicit scoping for elements in the std namespace (e.g., std::cout instead of just cout). This helps avoid certain kinds of namespace conflicts, especially later when you start woring with istream and ostream selector operators. It isn't required, but it is a good practice to get into.

I am using Code::blocks 13.12.
the algorithm is-

initialize an empty stack
while there is item to read from the string, do
    if item is an opening parentheses, then
          push it into the stack
     else if item is a closing parentheses, then
          if stack is empty, then
                string is not balanced
           else
                pop from stack
                if popped item does not corresponds to the closing                                          parentheses, 
                     then string is not balanced
if stack is non-empty, then
         string is not balanced
else 
   string is balanced

here's my full code:

 //itemtype.h:::::
    #ifndef ITEMTYPE_H_INCLUDED
    #define ITEMTYPE_H_INCLUDED

    class itemtype
    {
    public:

        Itemtype();
        void print()const;
        void initialize(int number);

    private:

        int value;
    };

    #endif // ITEMTYPE_H_INCLUDED




    //itemtype.cpp::::
    #include "itemtype.h"
    #include <iostream>
    using namespace std;

    itemtype::Itemtype()
    {
        value=0;
    }

    void itemtype::initialize(int number)
    {
        value=number;
    }

    void itemtype::print()const
    {
        cout<<value<<" ";


    }




    //stack.h:::::
    #ifndef STACK_H_INCLUDED
    #define STACK_H_INCLUDED
    #define MAX_ITEMS 5
    #include "itemtype.h"

    class stack
    {
    public:
        stack();
        bool isEmpty()const;
        bool isFull()const;
        void push(itemtype);
        void pop();
        itemtype Top();
        itemtype topPop();
        void printStack();

    private:
        int top;
        itemtype info[MAX_ITEMS];


    };



    #endif // STACK_H_INCLUDED




    //stack.cpp::::
    #include "stack.h"
    #include "itemtype.h"
    #include <iostream>
    using namespace std;

    stack::stack()
    {
        top=-1;
    }

    bool stack::isEmpty()const
    {
        return(top==-1);
    }

    bool stack::isFull()const
    {
        return (top==MAX_ITEMS-1);
    }

    void stack::push(itemtype item)
    {
        if(isFull())
            cout<<"Stack is full"<<endl;
        else
        {
            top++;
            info[top]=item;
        }
    }

    void stack::pop()
    {
        if(isEmpty())
            cout<<"Stack is Empty"<<endl;
        else
        {
            top--;
        }
    }

    itemtype stack::topPop()
    {
        itemtype it=Top();
        pop();
        return it;
    }

    itemtype stack::Top()
    {
        return info[top];
    }

    void stack::printStack()
    {
        itemtype it;
        while(!isEmpty())
        {
            it=Top();
            it.print();
            top--;
        }
        cout<<endl;
    }




    //main::::
    #include <iostream>
    #include "itemtype.h"
    #include "itemtype.cpp"
    #include <fstream>
    #include "stack.h"
    #include "stack.cpp"
    using namespace std;


    int main()
    {
       stack c;

       ifstream inFile("parentheses.txt");
       ofstream outFile("report.txt");

       int i, N;

       char str[500];

       itemtype a1;
       a1.initialize(i);

       inFile >> N;
       inFile >> str;

      while (str[i])
    {
        for (i = 0; i<N; i++)
         {
          if ((str[i] == '(') || (str[i] == '{') || (str[i] == '['))
            {
              c.push(a1);
            }

          else if ((str[i] == ')') || str[i] == '}' || str[i] == ']')
            {
            if (c.isEmpty() == 1)
              outFile << "Parentheses are not Balanced" << endl;

            else if (
                      (str[i] == ')'  && str[i] == ')') ||
                      (str[i] == '}'  && str[i] == '}') ||
                      (str[i] == ']'  && str[i] == ']') )
            {
              c.pop();

            }
            else
              outFile << "Parentheses are not Balanced" << endl;
            }
         }
        i++;
     }

      if (c.isEmpty() == 1)

        outFile << "Parentheses are Balanced" << endl;

      else

        outFile << "Parentheses are not Balanced" << endl;
    }

can anyone please check it out? and correct it? Thanx in advance :)

As Schol-R-Lea showed (but may not have pointed out), you were, at the least, missing the leading left-bracket when defining your class. IE, you posted

class itemtype
public:
.
.
.
};

but it should have been

class itemtype {
public:
.
.
.
};

I have corrected that. but the error is in other part may be :(

This related example may help ...

may give you some ideas ?

// classStack.cpp //


#define MAX_ITEMS 50

template < typename T >
class Stack
{
public:
    Stack();
    bool isEmpty() const;
    bool isFull() const;
    void push( const T& );
    void pop();

    T top() const;
    T topPop();

    int size() const;

    void printStack() const;
private:
    int top_index;
    T info[MAX_ITEMS];
} ;



#include <iostream>

template < typename T >
Stack < T >::Stack()
{
    top_index = -1;
}

template < typename T >
bool Stack < T >::isEmpty() const
{
    return( top_index == -1 );
}

template < typename T >
bool Stack < T >::isFull() const
{
    return( top_index == MAX_ITEMS-1 );
}

template < typename T >
void Stack < T >::push( const T& item)
{
    if( isFull() )
        std::cout << "Stack is full" << std::endl;
    else
    {
        ++top_index;
        info[top_index] = item;
    }
}

template < typename T >
void Stack < T >::pop()
{
    if( isEmpty() )
        std::cout << "Stack is Empty" << std::endl;
    else
        --top_index;
}

template < typename T >
T Stack< T >::top() const
{
    if( !isEmpty() )
        return info[top_index];
    else
    {
        std::cout << "Stack is Empty" << std::endl;
        return T(0);
    }
}

template < typename T >
T Stack < T >::topPop()
{
    T it = top();
    pop();
    return it;
}


template < typename T >
int Stack< T >::size() const
{
    return top_index;
}

template < typename T >
void Stack< T >::printStack() const
{
    int i = top_index;
    while( i >= 0 )
    {
        std::cout << info[i] << ' ';
        --i;
    }
}

bool match( char lead, char trail )
{
    if( lead == '(' && trail == ')' ) return true;
    if( lead == '{' && trail == '}' ) return true;
    if( lead == '[' && trail == ']' ) return true;

    // else ... if reach here ...
    return false;
}

char getMatchingLeadChar( char trail )
{
    if( trail == ')' ) return '(';
    if( trail == '}' ) return '{';
    return '[';
}


#include <cstring> // re. strlen
void testAndShow( const char* str )
{
    using std::cout;
    using std::endl;

    int len = strlen( str );

    cout << "len = " << len << endl;

    Stack < char > myStack;

    int i = 0;
    while( str[i] )
    {
        char c = str[i];

        if( c == '(' || c == '{' || c == '['  )
        {
            myStack.push( c );
        }
        else if( c == ')' || c == '}' || c == ']' )
        {
            if( !myStack.isEmpty() )
            {
                if( match(myStack.top(), c) ) // ok so far
                {
                    myStack.pop();
                }
                else
                {
                    cout<< myStack.topPop() << " and "
                        << c
                        << " parentheses are not balanced."
                        << endl;
                }
            }
            else
            {
                cout << "Missing leading "
                     << getMatchingLeadChar( c )
                     << " parentheses are not balanced with "
                     << c  << " found here." << endl;
            }
        }

        ++i;
        cout << "i = " << i << endl;
    }

    cout << "\nPrinting the stack as it is now: \n";
    myStack.printStack();
    cout << endl;

    while( !myStack.isEmpty() )
    {
        cout << "Extra leading " << myStack.topPop()
             << endl;
    }
}


using namespace std;



int main()
{
    const char* str = "( () [] {}  ( { [ ] } ) ) )";
    testAndShow( str );
    cout << "\nPress 'Enter' to continue... " << flush;
    cin.get();

    const char* str1 = "(  ( { {} ] } ) ) )";
    testAndShow( str1 );
    cout << "\nPress 'Enter' to continue... " << flush;
    cin.get();

    const char* str2 = "( () () ) } ]";
    testAndShow( str2 );
    cout << "\nPress 'Enter' to continue... " << flush;
    cin.get();

    const char* str3 = "{[( () () )";
    testAndShow( str3 );

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.