Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

two syntax-related C++ questions

Posted on 1998-05-12
14
Medium Priority
?
274 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
ID: 1176144
Just forgot to mention that I am using VisualC++ 5.0
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176145
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176146
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Expert Comment

by:nietod
ID: 1176147
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
ID: 1176148
Man, I can't think or TYPE today.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1176149
Try:
  void append(const T& obj);

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176150
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
 
LVL 11

Expert Comment

by:alexo
ID: 1176151
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
ID: 1176152
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
ID: 1176153
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
ID: 1176154
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
ID: 1176155
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
ID: 1176156
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 120 total points
ID: 1176157
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

916 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