Solved

two syntax-related C++ questions

Posted on 1998-05-12
14
217 Views
Last Modified: 2012-06-21
Hi,

  I am a little bit confused on some syntax in C++, yet I do not have a C++ reference book at hand... So please help if you can...
  I declared my class (Vector) like following:

template <class T>
class Vector  
{
public:
...
void append(T& obj);  // add one element to the end of vector
T& operator[](long index);
...
};

The implementation is omitted('cos I don't think it is related to the problem).

The following code attemps to use the Vector class:

Vector<int> myVector();
myVector.append(3);
printf("%d\n",myVector[0]);


However, I got the following two errors:
error C2664: 'void Vector<int>::append(int &)' : cannot convert parameter from 'const int' to 'int &'

error C2666: '[]' : 2 overloads have similar conversions


Can anyone tell me how to correct these errors? Thanks!

wyldsj
0
Comment
Question by:wyldsj
  • 7
  • 3
  • 2
  • +1
14 Comments
 

Author Comment

by:wyldsj
Comment Utility
Just forgot to mention that I am using VisualC++ 5.0
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I thought I knew what the problem was, but now I'm not so sure.  This is what I SUSPECT, but I couldn't find documentation to back it up:

I suspect that procedures that take variable numbers of arguments, like printf(), can't take reference parameters.  That is they can take "int", but not "int &".  This makes sense knowing what I know about the internals of these functions.  However, it should convert "int &" to "int" so there should be no problem.  (I thought it would not do the convesion, but I now think it should.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
As for the "2 overloads have similar conversions" error this occurs when there are two overloaded functions that can't be distinguished from each other, like
class someclass
{
  void F(char c);
  void F(int i);
}

If you do something like

char aChar;
  F(aChar);

will be fine, but if you do.

F(3);

the compiler doesn't know what F() you want.  Is it char(3) or int(3)?

If you post more of your code I might be able to help with that.  I'm not sure I can help with your first problem at this time, however.  

One test we could try (for the first problem) is to copy the return value to a non-reference variable like,

Vector<int> myVector();
int i;
myVector.append(3);
i = myVector[0]
printf("%d\n",i);

I woudl be cuyrious to know what affect that has.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Man, I can't think or TYPE today.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Try:
  void append(const T& obj);

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Oh man.  I was thinking it was the operator [] that had the error.  It was the append().  Alexo should get these points (and I should quit).
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:alexo
Comment Utility
Relax Todd, even people who have been in the business since the age of punched cards and the wooly mammoth can err once in a while.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
How long am I going to have to hear about this?  

Anyone ever tell you your timing is rotten?   I was hoping you or somone would support me on those questions with john.  On two questions he would not listen to me about that bug that was clearly there and no one said a word.  So I finally bragged about my experience to get him to listen--then you comment.  
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
The problem is that you cannot get a non-const reference (int&) to an integer constant (int).  Such a reference would allow you to change the constant .. which is a no-no.

Don't use reference args (&) without a const UNLESS you are _really_ going to change the args inside the function.  Similarly, always make your member functions 'const' unless the really do (or can) change the object.

So in your case you should change
    void append(T& obj);
    T& operator[](long index);
into
    void append(const T& obj);
    T& operator[](long index);
    const T& operator[](long index) const;

NOTE: you should have both a const and no-const operator[] so that you can use it on a const array object without problems.

Also, it looks like you must have two operator[] functions.  Perhaps one that takes an int and one that takes a long?  That would explain your second error (it is not explained by the missing const).


BTW: nietod: What are "those questions with john" you talk about.  Can I help here?  [It's always good to be supportive of the elderly ;-)]

0
 

Author Comment

by:wyldsj
Comment Utility
Hi nietod,

  Sorry but I think Ronslow's answer is more appropriate, so I think it will be fair to give the credit to him :-)

Ronslow:
  I did as what you said for the 'const' thing and it worked. However, for the operator overloading, I have only one overloading function so it is not the problem that you mentioned. However, the problem disspeared after I use the 'Vector' class in this way:  printf("%ld\n",myVector(2L));

  Anyway, please re-submit ur comments as answer and I will give u the credit. Thanks.
  BTW, i will be grateful if you could explain a little bit on what a 'const' function is, e.g.
int myfunction(void) const;
What does that const mean?

wyldsj

0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
wyldsj, the "const" qualifier says that the member function (a.k.a. method) is guaranteed not to change the object it is invoked on.  Only const methods can be invoked on const objects.

Example:

    class X
    {
    public:
        Func()
        ConstFunc() const;
    }

    X obj;
    const X constObj;

    obj.ConstFunc(); // OK
    obj.Func(); // OK
    constObj.ConstFunc(); // OK
    constObj.Func(); // Compilation error - Func may change a const object.

Now, to other issues:

Sorry Todd, I just thought it was funny.  Obviously you didnt, so I hereby present my sincere appologies and solemnly promise never to poke fun at your age again.  (Timing?  What timing?  I'm totally async.)

>> I was hoping you or somone would support me on those questions with john.
Please remember that I'm 7 time zones from the east coast.  When I got to that question, it was already too late.  Anyway, john seems to agree with you now.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Alex,  I thought it was funny too.  But now you got Roger involved!

Roger, thanks for the offer.  But John is now listening to me and the bug is gone.  He just didn't want to believe me about a bug.

wyldsj, Sorry, I couldn't help you.  I wasn't thinking clearly.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 30 total points
Comment Utility
regarding const members.

If a member function is const (place the keyword 'const' after the closing ')' in the function header then that function is not allowed to change any of the member variables of the object.  It is also not allowed to call any other non-const member functions of the class (because _they_ could change member variables).

Also, a const object (eg, you passed an object to a function as a const&) cannot call a non-const member function.

So .. in summry, you can only call const member functions from a const object or another const member fucntion; and you cannot modify member variables in a const function.

EXCEPTION: you can make a member variable 'mutable' .. in which case it CAN be modified by a const function.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now