Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   String argument (http://www.programmingforums.org/showthread.php?t=7155)

-=PARADOX=- Nov 23rd, 2005 11:19 AM

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 ;)

DaWei Nov 23rd, 2005 11:50 AM

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?

-=PARADOX=- Nov 23rd, 2005 12:04 PM

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... ;)

Kilo Nov 23rd, 2005 3:02 PM

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?

DaWei Nov 23rd, 2005 3:16 PM

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 .

Kilo Nov 23rd, 2005 4:01 PM

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 :)

DaWei Nov 23rd, 2005 4:21 PM

:

myFunction (mytype& label)

myFunction (mytype & label)

myFunction (mytype &label)

myFunction (mytype&)

etc.

That do? They don't go in front of the variable type in a declaration or definition.

Kilo Nov 23rd, 2005 5:19 PM

lol wierd, i will stick with my pointers :)


All times are GMT -5. The time now is 11:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC