Thread: expected `)' before str... help!

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    expected `)' before str... help!

    Almost got everything debugged in my code, but I'm having this error:
    integer.h:9: error: expected `)' before ‘astr’

    Header:
    Code:
    #ifndef INTEGER_H
    #define INTEGER_H
    
    class Integer{
     private:
      int _int;
     public:
      Integer(int i);
      Integer(std::string astr);
    
      int getValue();
      void setValue(int i);
      void add(Integer& i);
      void subtract(Integer& i);
      void multiply(Integer& i);
      void divide(Integer& i);
    };
    #endif
    Cpp:
    Code:
    #include <iostream>
    #include <string>
    #include "integer.h"
    
    Integer::Integer(int i){
      _int = i;
    }
    
    Integer::Integer(std::string astr){
      _int = atoi(astr.c_str());
    }
    
    int Integer::getValue(){
      return _int;
    }
    
    void Integer::setValue(int i){
      _int = i;
    }
    
    void Integer::add(Integer& i){
      _int += i.getValue();
    }
    
    void Integer::subtract(Integer& i){
      _int -= i.getValue();
    }
    
    void Integer::multiply(Integer& i){
      _int *= i.getValue();
    }
    
    void Integer::divide(Integer& i){
      if(i.getValue() !=0 ) _int /= i.getValue();
      else {
        std::cout << "Cannot divide by zero!\n";
        exit(-1);
      }
    }
    What is causing this issue??

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    works fine here (gcc 4.2.3, 64-bit Linux).

    By the way, identifiers starting with an underscore are by convention reserved for implementation's internal use (compilers and standard libraries).
    Last edited by cyberfish; 05-14-2008 at 10:22 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    Killed the underscore, change it to myInt. Still doesn't work.
    This is amazing. Why won't this work on my system???

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    what compiler?

    Also, post the whole compiler output.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    7
    I'm using g++ (GCC) 4.1.2 20070626.

    New to c++ and g++, what would the whole compiler output include? that is the only error i have.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    hmm that is strange

    what command did you use to compile?
    I used
    Code:
    g++ -c integer.cpp
    since integer.cpp doesn't have a main().

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Maybe your string header file is messed up.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    You probably miss the header file 'string'.
    Just put the line
    Code:
    #include <string>
    after the "#define INTEGER_H"

    Ah, I see you've only included it in the .cpp file. This means you cannot use types from this header in your .h file. Include it in the .h file instead and it will work.

    Your functions 'add', 'subtract', 'multiply', 'divide' are in fact operators '@=' instead of functions. Consider the following:

    Code:
    #ifndef INTEGER_H
    #define INTEGER_H
    
    #include <string>
    
    class Integer{
     private:
      int _int;
     public:
      Integer(int i);
      Integer(std::string astr);
    
      int getValue() const;           // const declaration
      void setValue(int const& i); // const declaration of argument 
    
      Integer& operator +=(Integer const& i);
    };
    #endif
    Code:
    inline
    int Integer::getValue() const
    {
      return _int;
    }
    
    Integer& Integer::operator+=( Integer const& p_int)
    {
      this->_int += p_int.getValue();
      return *this;
    }
    
    #endif
    Now you can implement an operator+ accordingly:

    Code:
    Integer operator+( Integer const& p_arg1, Integer const& p_arg2)
    {
      Integer result( p_arg1);
      return ( result += p_arg2);
    }
    Last edited by MarkZWEERS; 05-14-2008 at 11:08 PM.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MarkZWEERS View Post
    Ah, I see you've only included it in the .cpp file. This means you cannot use types from this header in your .h file. Include it in the .h file instead and it will work.
    In this case <string> was included first, so its definitions are available to the other header. Not the best practice, but also not the cause of the problem.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think it probably is the cause of the problem.

    If there is another source file that uses the Integer class but doesn't #include <string> then you will get that exact error when you compile the file.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    I think it probably is the cause of the problem.

    If there is another source file that uses the Integer class but doesn't #include <string> then you will get that exact error when you compile the file.
    So the problem report was incomplete. Wouldn't be the first time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Windows using Dev-C++
    By Renegade in forum C++ Programming
    Replies: 15
    Last Post: 07-07-2005, 08:29 PM