Thread: Including a custom class file

  1. #16
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    The full error message is:
    Code:
    1 N:\USERS\bakewens\CS240\Program 2\myStack.cpp expected unqualified-id before '[' token 
    1 N:\USERS\bakewens\CS240\Program 2\myStack.cpp expected `,' or `;' before '[' token
     N:\USERS\bakewens\CS240\Program 2\Makefile.win [Build Error]  [myStack.o] Error 1
    The line number is 1.

    The other computer will just be using gcc through a terminal window command prompt. How would you link them together on the command line?

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't remember. Do a man gcc and it should give you the options you need. You might just need to compile them on the same line> g++ stackDriver.cpp myStack.cpp, but like I said I don't remember.

    As to your current error, that's pretty strange. It's compiling as a C++ project? How did you create the project?

  3. #18
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    I just compiled it again and the error went away...I haven't changed anything :-\ Hmm!

    I have a different question now: getline returns -1 on EOF, so an appropriate loop to read all of the contents of a file until the file is done would be this, correct?

    Code:
    while(getline(cin, stack1.aa[stack1.cc]) != -1) {
    edit: i just compiled that and it gave me this error:

    Code:
    main.cpp no match for 'operator!=' in 'std::getline [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>][lot more stuff]
    but this is pretty much the same example as linuxmanpages.com has.

    By the way, on Dev-C++, how do I include a file as my input stream and not the keyboard? I tried add "<inputFile.txt" in Parameters under Execute, but that didn't work. The dev-c++ site is not very helpful, either.
    Last edited by Beowolf; 09-27-2007 at 04:22 PM.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, getline does not return -1 on EOF. If the read attempt fails, it will evaluate to false (that doesn't necessarily mean EOF, but that doesn't matter). So you should do this:
    Code:
    while(getline(cin, stack1.aa[stack1.cc])) {
    Of course, that's not exactly how a stack usually works. Generally, you would read into a temporary variable and then call a Push function to push the value onto the stack.

  5. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ok, thanks, it compiles now. Linuxmanpages.com says about getline:
    Both functions return -1 on failure to read a line (including end of file condition).
    Oh well.

    Any idea on to why I include "<testInput.txt" in the command line parameters the program does not read the file as the input stream and still waits for keyboard entry? My parameter list looks like this:

    Code:
    5 <testInput.txt
    where 5 is the size of the stack, and I know that the file exists in the current working directory

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That man page is for a completely different function that goes by the same name (notice the parameters are different than what you're using). I'd use a different reference for you C++ programming. Perhaps a book, or cppreference.com or dinkumware.com.

    As for the redirect issue, I would expect that the end of the redirected file should cause the getline to stop running. Can you post your updated code?

  7. #22
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Argh! That "expected unqualified-id before '[' token" is back for no apparent reason.

    My new code:

    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <ctype.h>
    #include <stdlib.h>
    #include "myStack.h"
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
        myStack stack1;
        int arg_length;
        char current_digit;
    
    
        if(argc < 2) {
            cerr << "Bad command line argument.\n";
            exit(1);
        }
    
        arg_length = strlen(argv[1]);
    
        if(arg_length > 9) {
            cerr << "Bad command line argument.\n";
            exit(1);
        }
        
        for(int ii = 0; ii < arg_length; ii++) {
            current_digit = argv[1][ii];
            if(!(isdigit(current_digit))) {
                cerr << "Bad command line argument.\n";
                exit(1);
            }
        }
    
        stack1.setStackSize(atoi(argv[1]));
    
        stack1.cc = 0;
        while(getline(cin, stack1.aa[stack1.cc])) {
            if(stack1.aa[stack1.cc].length() > 80)
                cerr << "Bad input.\n";
            else if(stack1.aa[stack1.cc][0] == '<')
                stack1.pop(stack1.cc);
            else if(stack1.aa[stack1.cc][0] == '>')
                stack1.push(stack1.cc);
            else if(stack1.aa[stack1.cc].length() == 0)
                exit(0);
            else
                cerr << "Bad input.\n";
        }      
        
    
        system("Pause");
        return 0;
    }
    myStack.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "myStack.h"
    
    using namespace std;
    
    void myStack::setStackSize(int x) 
    {
    
        stackSize = x;
    
        if(!(aa = new(nothrow) string[stackSize+1])) 
            cerr << "Program cannot handle a stack of size " << stackSize << ".\n";  
       
    }
    
    
    void myStack::pop(int p) {
        if(p == 0)
        {
            cout << "Empty stack.\n";
        }
        else
        {
            cout << aa[p-1];
            cc--;
        }
    }
    
    
    void myStack::push(int p) 
    {
        if(p == stackSize) 
        {
            cerr << "Full stack.\n";
        } 
        else 
        {
            aa[p] = aa[p].substr(1, aa[p].length());
            aa[p] += "\n";
            cc++;
        }
    }
    myStack.h
    Code:
    /* myStack.h */
    #ifndef HEADER_H
    #define HEADER_H
    #include <string>
    
    class myStack
    {
        public:
            std::string (*aa);
            int stackSize;
            int cc;
            void setStackSize(int);
            void pop(int);
            void push(int);
            int displayStackSize();
    };
    #endif

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    And finally, post your input file.

  9. #24
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Code:
    <
    >hello
    <
    >hi there
    <

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It works for me. This is the output I get:
    Code:
    D:\test\Debug>test.exe 5 <testInput.txt
    Empty stack.
    hello
    hi there
    Press any key to continue . . .
    I'm using VC++ 7.1 and I run it from a command prompt.

  11. #26
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    I ran it through the command prompt and it worked fine, too. I guess it is just an issue with Dev-C++.

    About the compile error that I get sometimes ("expected unqualified-id before '[' token"): when I get it and then I switch around the #include positioning in my file (for example just switch #include <iostream> down one) the error goes away for a while. Google has practically no results when I search about this.

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Question, though: why do you include both <cstdlib> and <stdlib.h>? Also, you should use <cctype> instead of <ctype.h>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm guessing the "expected unqualified-id before '[' token" error is just something wrong with your configuration, but who knows.

    I could only run it from the command prompt as well. VC++ had issues with redirected input from its command-line settings.

  14. #29
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Quote Originally Posted by CornedBee View Post
    Question, though: why do you include both <cstdlib> and <stdlib.h>? Also, you should use <cctype> instead of <ctype.h>.
    Is including both cstdlib and stdlib repetitive? Is there a better choice to include one or the other, or does it not really matter which one I choose?

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    cstdlib is the true C++ header. It is defined to have the same contents as the C header stdlib.h, but with all elements except the macros declared inside namespace std, as everything in the standard library should be.
    stdlib.h is the compatibility header. In addition to doing everything the cstdlib header does, it also imports all symbols of the header into the global one, so that programs written with the C header in mind continue to work.

    So, in theory, cstdlib doesn't pollute your global namespace that much.

    In practice, it is somewhat different: compilers tend to pack C and C++ in a single package, and there's hardly a difference between stdlib.h and cstdlib. cstdlib is often implemented by including stdlib.h and then importing the elements into the std namespace. This is, strictly speaking, non-conformant, because it pollutes the global namespace. In addition, I know that some compilers used to not present the symbols in the std namespace when stdlib.h was included - VC++6, for example, didn't.

    However, in modern C++ programs you should include the cname versions of all the inherited headers, not the name.h versions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM