![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
String argument
I have this class 'CMatrix':
class CMatrix
{
CVector *matrix;
unsigned lin, col;
string m_sNome;
public:
CMatrix( string&, unsigned, unsigned );
~CMatrix();
};And I want to pass a string to the constructor... but my compiler says "string cannot start a parameter declaration"... What's wrong? Thanks in advance ![]() |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The code shown compiles without error, for me, using VC++ 6.0 and VC++ 2005. I didn't try Borland. I did comment out "CVector *matrix;" rather than worry about its definition. I also can't find an error that conforms to your wording, by that could be different compilers. Does the error actually occur/point at what you are showing?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
Nevermind... since my prog is in various modules, I forget to put in the 'CMatrix.h':
#include <iostream> using namespace std; Thanks for the time lost... ![]() |
|
|
|
|
|
#4 |
|
Expert Programmer
|
and this raises a question from me!
CMatrix(string&) {;}please explain to me in psuedocode what happens when you write the ampersand AFTER the variable type and pass it to a constructor? personally i would have just wrote CMatrix(string); i guess my questions are! First: why write the ampersand after the variable type? and Second:how would your program benefit from this?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In C++, as shown, the ampersand represents a "reference". That's probably an unfortunate choice of terms by the language writers, as pointers, your pet name for your sweetheart, plain labels, and other items are still technically (and correctly designated as) references. Nevertheless, it's a special thangy in C++. It is, in fact, an alias. It is just another name for the original, not a copy, not an address-book entry, or any of that. When a reference is passed to a function, the argument is actually a pointer of a sort, but there is one less level of indirection. Consequently, usage inside the function has a different syntax. When you dink with the reference, you modify the original, just as you do when you mess with a pointer's target, but, as I say, with one less level of indirection. You may go to my pointer tutorial if you like, but I promise you, you'll not find them covered there :p .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Expert Programmer
|
ok yes im familiar with reference's. I just have never seen them refered to with the ampersand after the variable type. With functions and parameters aside, when i refer to a reference would i always place the ampersand after the variable name? May i see some examples of this
![]()
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
myFunction (mytype& label) myFunction (mytype & label) myFunction (mytype &label) myFunction (mytype&) etc.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Expert Programmer
|
lol wierd, i will stick with my pointers
![]()
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|