two syntax-related C++ questions

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
wyldsjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wyldsjAuthor Commented:
Just forgot to mention that I am using VisualC++ 5.0
0
nietodCommented:
answer coming.
0
nietodCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

nietodCommented:
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
nietodCommented:
Man, I can't think or TYPE today.
0
alexoCommented:
Try:
  void append(const T& obj);

0
nietodCommented:
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
alexoCommented:
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
nietodCommented:
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
RONSLOWCommented:
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
wyldsjAuthor Commented:
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
alexoCommented:
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
nietodCommented:
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
RONSLOWCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.