Solved

two syntax-related C++ questions

Posted on 1998-05-12
14
246 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 30 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 422
is twain_32.dll cmpatible with windows 10 ? 10 183
Best book to learn C++ 4 83
Arduino EDI - Programming language 3 103
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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