Thread: weird compilation error

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    17

    weird compilation error

    hi all...
    i have a weird compilation error... when i declared an object

    |error: no matching function for call to `Stack::Stack()'| !!!!!!

    this is the code!!

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Stack {
    
        private:
    
        char *stackArr;
        int Size;
        int top;
    
    
        public:
    
        Stack (int s)
        {
            stackArr= new char[s];
            Size= s;
            top= -1;
        }
    
        ~Stack ()
        {
            delete[] stackArr;
        }
    
        bool Push (char c)
        {
            if(top == Size-1)   // stack is full
            return false;
    
            else
            {
                top++;
                stackArr[top]= c;
                return true;
            }
        }
    
        bool Pop (char & c)
        {
            if(isEmpty())
            return false;
    
            else
            {
                c= stackArr[top];
                top--;
                return true;
            }
        }
    
        bool Peek (char & c)
        {
            if(top==-1)
            return false;
            c= stackArr[top];
            return true;
        }
    
        bool isEmpty ()
        {
            return top== false;
        }
    };
    
    
    
    int main()
    {
        Stack S,St;
        ifstream inFile;
        ofstream outFile;
        char c;
        int counter=0;
    
        inFile.open("File1.txt");
        if(!inFile)
        {
            cout<<"UNABLE TO OPEN THIS FILE!!! "<<endl;
            exit(1); // terminated with error
        }
        else
    
        Stack(100);
        while(inFile!=NULL)
        {
            inFile.get(c);
            S.Push (c);
            counter++;
        }
        outFile.open("File2.txt");
        while(!outFile)
        {
            if(!S.isEmpty())
            {
                S.Pop(c);
                outFile<< S.Peek(c);
                cout<<endl;
            }
        }
    
    
    
            return 0;
    }
    and also i have a question...

    i want to read from a text file then copy every character into a stack after that i have to save it in another text file in reverse order

    who could teach me how can i save it in reverse order please ?!!

    i'm new in stack and really i have problem with using it

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    It's probably complaining about this line since you have specified a constructor that takes an int but not one that doesn't take any arguments.
    Code:
    Stack S,St;
    Also, what is this line supposed to do?
    Code:
    Stack(100);

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Quote Originally Posted by Memloop View Post
    It's probably complaining about this line since you have specified a constructor that takes an int but not one that doesn't take any arguments.
    Code:
    Stack S,St;
    Also, what is this line supposed to do?
    Code:
    Stack(100);


    100 is the size of the stack ! is it wrong ! :s

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That stack is created and immediately destroyed, since it is a temporary.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ladybird__86 View Post
    100 is the size of the stack ! is it wrong ! :s
    There's nothing wrong with the 100. It's just that the statement is like saying:
    Code:
    int(100);
    To which one would say "Sure okay 100 is an int ... whoopdeedoo!"
    Something's missing!

    Also, by "the rule of three", your stack class is broken.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot create a class and initialize it later. You must initialize it directly when you create it, so it should be:
    Stack S(100)

    If you do not know the size at the time you create it, you can delay creating it until after you know the size.
    If that is not possible, then you have to make a pointer to the class and create it later when you need it with new and initialize it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Quote Originally Posted by Elysia View Post
    You cannot create a class and initialize it later. You must initialize it directly when you create it, so it should be:
    Stack S(100)

    If you do not know the size at the time you create it, you can delay creating it until after you know the size.
    If that is not possible, then you have to make a pointer to the class and create it later when you need it with new and initialize it.

    thank you,,,
    yes you r right i have to initialize the size first

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM