problems with std and const (inspectors)

D

david

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>
 
C

Christopher

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.
 
V

Victor Bazarov

david said:

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
 
D

david

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?
 
V

Victor Bazarov

david said:
[..]
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
 
D

david

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.
 
D

david

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.
 
D

david

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?
 
D

david

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.
 
V

Victor Bazarov

david said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top