Loans | Free Ringtones | Houses for Sale | Modded Xbox | Buy PSP
yay, classes.... [Archive] - PCMech Forums

PDA

View Full Version : yay, classes....


GSXdan
09-22-2003, 08:53 PM
so we have started classes in my C++ class, and they seem pretty easy, i have the concept, but i keep getting this error message and i cant figure it out. Im about 99% sure it has to do w/ me sending a char string though.

class:

class Package {

public:
Package(int=0, int=0, int=0, int=0, int=0);
void setDim(int, int, int);
void setWeight(int, int);
void setDestination(char[20]);
int getDirth();
int getWeight(int);
char getCity();

private:


int width;
int height;
int length;
int Wpounds;
int Wounces;
char City[];




};




function:



void Package:: setDestination(char dest[20]) {

cout << "hello" << endl;

//need to copy what comes in to the City object

}




main/call:




Package toBeShipped[100];
ifstream inf;
int l, w, h, p, o;
int numPacks, x;
char dest[20];

numPacks=0;

inf.open("lib/packages.dat");


while(!inf.eof()) {

inf >> w >> l >> h; // input length, width, and height

// call to setDim function to set dimenstions
toBeShipped[x].setDim(l, w, h);

inf >> p >> o; // input pounds and ounces

toBeShipped[x].setWeight(p, o); // call to setWeight to set weight


inf >> dest; // input destination

// call to set the destination
toBeShipped[x].setDestination(dest[20]);



numPacks++;
x++;

}

}





any help would be appreciated as always

thanks ^dan

GSXdan
09-23-2003, 03:39 PM
might be good if i posted the error message huh?


lab4.cpp: In function `int main()':
lab4.cpp:43: no matching function for call to `Package::setDestination (char &)'
package.h:20: candidates are: void Package::setDestination(char *)


^dan

panzerfaust
09-23-2003, 06:30 PM
assuming:

char dest[20];
Package p;

what you need to do is this:

p.setDestination(dest);

and not this:

p.setDestination(dest[20]);//dest[20] is a char not a char*

panzerfaust
09-23-2003, 06:32 PM
as a matter of fact you should try to get used to the notation char* instead of char[], it maybe a little tough to get used to the concept but it will make your life easier later

while you're at it do:
Package *p;
p->setDestination(dest);

just to be funny

GSXdan
09-23-2003, 06:54 PM
col cool it worked. once i get the whole program working i will try the pointer/arrow notation

thanks ^dan

doctorgonzo
09-24-2003, 09:04 AM
When working with arrays (like arrays of chars), it can be easy to screw up the use of the variable when passing it to functions. You just get used to it eventually. Working with pointers took me a while to figure out.

panzerfaust
09-24-2003, 01:51 PM
I guess the easiest way to think about char* is that the variable itself is the first element in the array and by doing variable++ or variable +1 or variable -1 you move around within the array. Simply realize that
foo[2] is the same as foo+2

LoveJones
09-26-2003, 01:14 PM
Just a little added information.

char foo[20];

If I just type "foo," it's type is actually a char*. And it points at foo[0] ( the first element ). It is much easier, much faster, and much more practical to use pointers when passing arrays =).