|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
yay, classes....
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: Code:
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: Code:
void Package:: setDestination(char dest[20]) {
cout << "hello" << endl;
//need to copy what comes in to the City object
}
main/call: Code:
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 |
|
|
|
|
|
#2 |
|
Member (10 bit)
|
might be good if i posted the error message huh?
Code:
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 *) |
|
|
|
|
|
#3 |
|
Member (3 bit)
Join Date: Sep 2003
Posts: 6
|
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* |
|
|
|
|
|
#4 |
|
Member (3 bit)
Join Date: Sep 2003
Posts: 6
|
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 |
|
|
|
|
|
#5 |
|
Member (10 bit)
|
col cool it worked. once i get the whole program working i will try the pointer/arrow notation
thanks ^dan |
|
|
|
|
|
#6 |
|
Professional gadfly
|
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.
|
|
|
|
|
|
#7 |
|
Member (3 bit)
Join Date: Sep 2003
Posts: 6
|
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 |
|
|
|
|
|
#8 |
|
Member (7 bit)
Join Date: Sep 2002
Posts: 93
|
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 =). |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|