![]() |
|
#1
|
|||
|
|||
Vector of user defined typeI am having some trouble inserting elements in a vector of a user defined type. Does anyone know what could be the issue? The error message I get is
"error C2558: class 'Rock' : no copy constructor available or copy constructor is declared 'explicit'" Thanks for your help in advance! =) CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: Vector of user defined typeYou haven't properly implemented your copy constructor (cctor).
CPP / C++ / C Code:
...is the signature. In your cctor "attempt," you are passing a Rock& not a const &. Therefore, you have not defined a cctor, only an overloaded ctor. :davis: |
|
#3
|
|||
|
|||
Re: Vector of user defined typeHi, thanks for your help. Using a constant reference as a parameter, I get the message
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Rock' (or there is no acceptable conversion) I tried to const_cast the const away, but the compiler does not seem to allow it. |
|
#4
|
|||
|
|||
Re: Vector of user defined typeQuote:
That's because the signature for an assignment operator also takes a const& and not a non-const reference. Maybe you should read up on const correctness? Imagine the idea of you doing something like: CPP / C++ / C Code:
The add operation has modified your values for a and b and returned something completely undesirable in c. In other words, had add taken a pair of const references for a and b, then we would know that it cannot/would not ever modify the values of a & b. In fact, we would have seen: Code:
...errors when compiling. However, in your own particular world, you decided that you would declare an operator that did not require a const&, so, since it is not the signature of the default assignment operator, your overridden operator applies. Using const_cast here is useless, because it is the container implicitly calling it, not your code directly. Here is a good idea. Learn what the signatures are for your basic class operations. Here is a quick, abbreviated example: CPP / C++ / C Code:
Of course, in a real "Fraction" class, we would also expect to find operator< and operator> implementations... :davis: |
|
#5
|
|||
|
|||
Re: Vector of user defined typeQuote:
Here is your Rock class more fully implemented: CPP / C++ / C Code:
Output: Code:
...of course, there is nothing to stop the user from entering a negative weight for a rock, but that's one of the interesting aspects of producing real-world code that you'll eventually come across...and have to deal with effectively! :davis: |
|
#6
|
|||
|
|||
Re: Vector of user defined typeThanks so much for your help. I didn't realise I was tripping myself up with the overloaded assignment operator, which I was declaring as part of a question requirement and did not intend to use. But I guess I've learnt the importance of using the correct function signatures when over-riding functions.
|
|
#7
|
|||
|
|||
Re: Vector of user defined typeQuote:
Something to remember when defining and implementing your own classes is that if you do not explicitly define and therefore implement ctor/dtor/cctor and assignment operator, the default actions of compiling will do it for you. You will not see these in your code, but you will see them in your object file(s). The following code will compile and execute: CPP / C++ / C Code:
This will compile and run, even though we have nothing in Foo. :davis: |
|
#8
|
|||
|
|||
Re: Vector of user defined typeOk, I'll take note of that. Thanks for your help =)
|
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Global Variables | Kalvorod | CPP / C++ Forum | 11 | 21-Feb-2007 12:07 |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 19:23 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| User defined headers | davis | Miscellaneous Programming Forum | 6 | 16-Feb-2006 18:40 |
| C++ PhoneBook | marita | CPP / C++ Forum | 46 | 12-Jun-2005 12:10 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The