Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problems with std and const (inspectors)

Reply
Thread Tools

problems with std and const (inspectors)

 
 
david
Guest
Posts: n/a
 
      02-26-2008
Files:
pirma_lib.h - http://www.paste.lt/paste/de66c8b700...19eea1318d1993
pirma_lib.cpp - http://www.paste.lt/paste/050b916516...8f54a0193fe564
pirma.cpp - http://www.paste.lt/paste/6753553152...703393d79d6f67


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>
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      02-26-2008
On Feb 26, 2:26 pm, david <(E-Mail Removed)> 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.






 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2008
david wrote:
> Files:
> pirma_lib.h -
> http://www.paste.lt/paste/de66c8b700...19eea1318d1993
> pirma_lib.cpp -
> http://www.paste.lt/paste/050b916516...8f54a0193fe564 pirma.cpp
> - http://www.paste.lt/paste/6753553152...703393d79d6f67


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[s].

> 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


 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-26-2008
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:
Macbookirma 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?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2008
david wrote:
> [..]
> error:
> Macbookirma 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


 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-27-2008
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.
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-27-2008
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.
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-27-2008
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?
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-27-2008
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:stream& operator<< (std: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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2008
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:stream& operator<< (std: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:stream& operator>> (std: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::list with const and non-const pointers - why aren't they typecompatible? arijit79@gmail.com C++ 4 03-02-2009 07:57 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
error: 'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private Geoffrey S. Knauth C++ 6 01-18-2006 11:48 AM



Advertisments