problems with std and const (inspectors)

Discussion in 'C++' started by david, Feb 26, 2008.

  1. david

    david Guest

    Files:
    pirma_lib.h - http://www.paste.lt/paste/de66c8b70084d3174d19eea1318d1993
    pirma_lib.cpp - http://www.paste.lt/paste/050b9165168c61cc168f54a0193fe564
    pirma.cpp - http://www.paste.lt/paste/6753553152fe92e939703393d79d6f67


    I am using std:: in pirma_lib everywhere and this why I do not need to
    use "using" and and std library which later could make some problems
    and it still does now.

    The problem is that when everything was in one file it was working
    perfect and now I decided to put everything in separate files. This
    way I made direct calls (std::) and removed "using namespace std;"
    from pirma_lib.cpp and this worked, I was able to compile this model (g
    ++ -Wall -ansi -pedantic -c pirma_lib.h);

    But I was not able to compile the main code. Every line where I was
    using "Aibe" class and it's methods was returning mistakes. But after
    commenting "using namespace std;" in pirma.cpp I did not get any
    error, but I should write to every cin, cout and etc std:: (make
    direct calls).

    The question would be how I should correctly move my class to separate
    file, make module? Why it throwing mistakes with that std (This one
    makes the most problems as I can see);

    - - -

    The second question.

    In pirma_lib.h there is a line: bool isSubSet(Aibe &other);
    But I would like to protect "other" Aibe instance and use "const Aibe
    &other", but inside it I am using length() method. (but I could easily
    get directly if I need). And this throws another mistake, that I can
    not use length() method in this case, but I then declare length: int
    length() cent; This should mean that length() can not change *this and
    I think that now it should allow me to call it, but again I am still
    getting mistake that it can not find such method at all. (It might be
    that I still does not understand how it works) Could someone explain
    why I can not call length() method?

    This mistake:
    pirma.cpp: In member function 'bool Aibe::isSubSet(const Aibe&)':
    pirma.cpp:101: error: no matching function for call to 'Aibe::length()
    const'
    pirma.cpp:20: note: candidates are: int Aibe::length() <near match>
     
    david, Feb 26, 2008
    #1
    1. Advertising

  2. david

    Christopher Guest

    On Feb 26, 2:26 pm, david <> wrote:
    > Files:
    > pirma_lib.h -http://www.paste.lt/paste/de66c8b70084d3174d19eea1318d1993
    > pirma_lib.cpp -http://www.paste.lt/paste/050b9165168c61cc168f54a0193fe564
    > pirma.cpp -http://www.paste.lt/paste/6753553152fe92e939703393d79d6f67
    >
    > I am using std:: in pirma_lib everywhere and this why I do not need to
    > use "using" and and std library which later could make some problems
    > and it still does now.
    >
    > The problem is that when everything was in one file it was working
    > perfect and now I decided to put everything in separate files. This
    > way I made direct calls (std::) and removed "using namespace std;"
    > from pirma_lib.cpp and this worked, I was able to compile this model (g
    > ++ -Wall -ansi -pedantic -c pirma_lib.h);
    >
    > But I was not able to compile the main code. Every line where I was
    > using "Aibe" class and it's methods was returning mistakes. But after
    > commenting "using namespace std;" in pirma.cpp I did not get any
    > error, but I should write to every cin, cout and etc std:: (make
    > direct calls).
    >
    > The question would be how I should correctly move my class to separate
    > file, make module? Why it throwing mistakes with that std (This one
    > makes the most problems as I can see);
    >
    > - - -
    >
    > The second question.
    >
    > In pirma_lib.h there is a line: bool isSubSet(Aibe &other);
    > But I would like to protect "other" Aibe instance and use "const Aibe
    > &other", but inside it I am using length() method. (but I could easily
    > get directly if I need). And this throws another mistake, that I can
    > not use length() method in this case, but I then declare length: int
    > length() cent; This should mean that length() can not change *this and
    > I think that now it should allow me to call it, but again I am still
    > getting mistake that it can not find such method at all. (It might be
    > that I still does not understand how it works) Could someone explain
    > why I can not call length() method?
    >
    > This mistake:
    > pirma.cpp: In member function 'bool Aibe::isSubSet(const Aibe&)':
    > pirma.cpp:101: error: no matching function for call to 'Aibe::length()
    > const'
    > pirma.cpp:20: note: candidates are: int Aibe::length() <near match>


    I am going to take a guess that you are passing a const reference to
    an object as a parameter to a function, which in turn is calling a
    method of that object which does not promise to not alter the data of
    the object.

    Aibe::length()
    needs to be Aibe::length() const;
    If the method does not alter Aibe's data

    If it does alter Aibe's data, than you can't pass a const object to
    Aibe::isSubSet, while Aibe::isSubSet calls Aibe::length.
     
    Christopher, Feb 26, 2008
    #2
    1. Advertising

  3. david wrote:
    > Files:
    > pirma_lib.h -
    > http://www.paste.lt/paste/de66c8b70084d3174d19eea1318d1993
    > pirma_lib.cpp -
    > http://www.paste.lt/paste/050b9165168c61cc168f54a0193fe564 pirma.cpp
    > - http://www.paste.lt/paste/6753553152fe92e939703393d79d6f67


    With all due respect, be aware that many people concerned with
    computer security are NOT going to follow some unverified links.
    It's simply too dangerous.

    Next time copy and paste your code right into your message. And
    spend some time reducing it to the bare minimum necessary to
    illustrate your question.

    > I am using std:: in pirma_lib everywhere and this why I do not need to
    > use "using" and and std library which later could make some problems
    > and it still does now.


    Not a clear statement. Separate into more than one sentence
    and review.

    > The problem is that when everything was in one file it was working
    > perfect and now I decided to put everything in separate files. This
    > way I made direct calls (std::) and removed "using namespace std;"
    > from pirma_lib.cpp and this worked, I was able to compile this model
    > (g ++ -Wall -ansi -pedantic -c pirma_lib.h);


    Why are you compiling what appears to be a header file?

    > But I was not able to compile the main code. Every line where I was
    > using "Aibe" class and it's methods was returning mistakes. But after
    > commenting "using namespace std;" in pirma.cpp I did not get any
    > error, but I should write to every cin, cout and etc std:: (make
    > direct calls).


    This is not really a C++ language question, but perhaps your .h
    file has all the necessary includes and your .cpp doesn't. Try
    including your .h file into your .cpp and compile your .cpp
    instead.

    > The question would be how I should correctly move my class to separate
    > file, make module? Why it throwing mistakes with that std (This one
    > makes the most problems as I can see);


    This is not really a language question.

    >
    > - - -
    >
    > The second question.
    >
    > In pirma_lib.h there is a line: bool isSubSet(Aibe &other);
    > But I would like to protect "other" Aibe instance and use "const Aibe
    > &other", but inside it I am using length() method. (but I could easily
    > get directly if I need). And this throws another mistake, that I can
    > not use length() method in this case, but I then declare length: int
    > length() cent; This should mean that length() can not change *this and
    > I think that now it should allow me to call it, but again I am still
    > getting mistake that it can not find such method at all. (It might be
    > that I still does not understand how it works) Could someone explain
    > why I can not call length() method?
    >
    > This mistake:
    > pirma.cpp: In member function 'bool Aibe::isSubSet(const Aibe&)':
    > pirma.cpp:101: error: no matching function for call to 'Aibe::length()
    > const'
    > pirma.cpp:20: note: candidates are: int Aibe::length() <near match>


    Make 'length' function also const. Correct its implementation
    to avoid making changes to *this object. You need to both declare
    and _define_ the 'length' function as 'const'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 26, 2008
    #3
  4. david

    david Guest

    Those links dangerous? I doubt it, nope I am 100% it is safe and do
    not worry about that.

    Sorry about that pirma_lib.h, that was typo it should be
    pirma_lib.cpp.

    The problem is that pirma_lib.cpp compiles fine with 0 errors and
    warnings, pirma_lib.h is included in pirma.cpp and I was not able to
    compile this codes, getting a lot of errors, huge amounts of it. I was
    thinking what could have caused it and I decided to comment line
    "using namespace std;" ir pirma.cpp and all errors there gone, but
    that would require always make direct calls to std library and I do
    not want that. The question is what could be causing this and why?
    What I did wrong moving my class to separate file? Because if I put
    everything in one file it works just perfect.

    And with those consts:

    int Aibe::length() const { // <- const
    return size;
    }

    bool isSubSet(const Aibe &other); // <- const
    void toArray(int **arr);
    std::string toString();
    int length() const; // <- const

    error:
    Macbook:pirma marius$ g++ -Wall -ansi -pedantic -c pirma_lib.cpp
    pirma_lib.cpp:52: error: prototype for 'bool Aibe::isSubSet(Aibe&)'
    does not match any in class 'Aibe'
    pirma_lib.h:11: error: candidate is: bool Aibe::isSubSet(const Aibe&)

    Or I am still missing something?
     
    david, Feb 26, 2008
    #4
  5. david wrote:
    > [..]
    > error:
    > Macbook:pirma marius$ g++ -Wall -ansi -pedantic -c pirma_lib.cpp
    > pirma_lib.cpp:52: error: prototype for 'bool Aibe::isSubSet(Aibe&)'
    > does not match any in class 'Aibe'
    > pirma_lib.h:11: error: candidate is: bool Aibe::isSubSet(const Aibe&)
    >
    > Or I am still missing something?


    Yes, of course. Did you read the error message? Compare the two
    declarations in it. Then compare the definition of 'isSubSet'
    with its declaration.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 26, 2008
    #5
  6. david

    david Guest

    I see and I know what the problem is, but I still can not understand
    how to fix it. How correctly I should write it.
     
    david, Feb 27, 2008
    #6
  7. david

    david Guest

    Thanks, just decided to check the code this morning and yes, now I
    noticed the problem.

    But the main and the biggest problem now is how to move my class to
    separate file, because this one still gives huge amounts of errors,
    almost on every line where I am using my "Aibe" class.
     
    david, Feb 27, 2008
    #7
  8. david

    david Guest

    Sorry for so many posts I am making (We could say I am just keeping
    you all up to date), the problem lies in my << overloaded operator.
    They I try to echo my class in this way:

    cout << "NNN : " << nnn << endl; // "<<" operator invokes toString()
    and worked fine then everything was in one place.

    Now it looks that compiler do not even check pirma_lib.h for this
    operator, but other operators seams to work. Any ideas why it ignores
    this one?
     
    david, Feb 27, 2008
    #8
  9. david

    david Guest

    I am really sorry about the amount of messages I am writing, but I am
    looking for answers and very curious.

    What I did is that I removed declaration of operator<< from Class and
    made it separate.

    std::eek:stream& operator<< (std::eek:stream &os, const Aibe &other); and
    this worked just fine.

    But I am still thinking could I put it inside, but then it requires
    only one parameter and I think that should be ostream, I am right?

    P.S. My first C++ program and I almost proud, the second version is
    going to be a lot more complex. Thanks all of you.
     
    david, Feb 27, 2008
    #9
  10. david wrote:
    > I am really sorry about the amount of messages I am writing, but I am
    > looking for answers and very curious.
    >
    > What I did is that I removed declaration of operator<< from Class and
    > made it separate.
    >
    > std::eek:stream& operator<< (std::eek:stream &os, const Aibe &other); and
    > this worked just fine.
    >
    > But I am still thinking could I put it inside, but then it requires
    > only one parameter and I think that should be ostream, I am right?


    Mmm... no.

    Think about it. To be the member of your class, the operator<< has
    to have its _left_ argument of your type, not the right argument.
    You could define

    std::eek:stream& operator>> (std::eek:stream& os) const;

    in your 'Aibe' class, but then the expression would look like

    yourAibeInstance >> cout;

    instead of

    cout << yourAibeInstance;

    It's no big deal, of course, but you'd still be unable to chain
    your output neatly. To output your class _and_ an int you'd have
    to write

    yourAibeInstance >> cout << int;

    To output an _int_ and your instance you'd have to write

    yourAibeInstance >> (cout << int);

    Both are ugly and hard to understand.

    > P.S. My first C++ program and I almost proud, the second version is
    > going to be a lot more complex. Thanks all of you.


    You're always welcome and best of luck in your journey!

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 27, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,050
    Earl Purple
    Jan 18, 2006
  2. Replies:
    11
    Views:
    1,153
  3. Javier
    Replies:
    2
    Views:
    628
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,174
    Tim Rentsch
    Nov 10, 2008
  5. Replies:
    4
    Views:
    2,711
Loading...

Share This Page