Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-22-2003, 07:53 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
GSXdan is offline   Reply With Quote
Old 09-23-2003, 02:39 PM   #2
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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 *)
^dan
GSXdan is offline   Reply With Quote
Old 09-23-2003, 05:30 PM   #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*
panzerfaust is offline   Reply With Quote
Old 09-23-2003, 05:32 PM   #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
panzerfaust is offline   Reply With Quote
Old 09-23-2003, 05:54 PM   #5
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
col cool it worked. once i get the whole program working i will try the pointer/arrow notation

thanks ^dan
GSXdan is offline   Reply With Quote
Old 09-24-2003, 08:04 AM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 09-24-2003, 12:51 PM   #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
panzerfaust is offline   Reply With Quote
Old 09-26-2003, 12:14 PM   #8
Member (7 bit)
 
LoveJones's Avatar
 
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 =).
LoveJones is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:06 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2