-4

So, I'm reading Schildt's book 3rd edition about C++ and I'm doing all examples, but I have some PHP background and when I tried some stuff it occurs that it can not be compiled this way.I saw the Schildt's solution, so I'll give what I've tried to do and how it's done in the book, what I need to know, is there any way to make it work adjusting my function?

Here's what I'm trying

class card {
char author[40];
//char book[30];
int count;
public:
    void store(char *auth,int ct);
    void show();
};
void card::store(char *auth,int ct){
    &author = *auth;

    count = ct;
}
int main(){
    card ob1, ob2;
    ob1.store('Tolkin',10);
    ob2.store('Pratchet',3);
    ob1.show();
    ob2.show();
return 0;
}

And here's the Schildt's solution:

class card {
char author[40];

int count;
public:
    void store(char *auth,int ct);
    void show();
};
void card::store(char *auth,int ct){
    strcpy(author, auth);

    count = ct;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Leron
  • 9,546
  • 35
  • 156
  • 257
  • 3
    9 months is long enough to learn how SO works and how to post quality questions. – Lightness Races in Orbit Dec 05 '11 at 16:53
  • It may be worthwhile to review c-strings and pointers again. – GWW Dec 05 '11 at 16:53
  • You should state more explicitly what your solution or Schildt's solution is solving as well as what errors you get when attempting to compile. This would enable others to help answer your question. – mindless.panda Dec 05 '11 at 16:54
  • 2
    If a book on C++ is teaching you to copy unvalidated input into a fixed-size buffer using `strcpy()`, then throw it away and get one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Mike Seymour Dec 05 '11 at 16:55
  • Always include the compiler error message in the post if you have a compilation problem. Did the compiler message give you any hints on where the problem is? – Anders Abel Dec 05 '11 at 16:55
  • 1
    Overheard long ago in a C newsgroup, "Schildt doesn't know schildt about C". I'm pretty sure that (lack of) knowledge has continued to C++. – KevinDTimm Dec 05 '11 at 17:02

4 Answers4

2

The quick fixes:

  • Instead of char author[40] use std::string.
  • store(const std::string& auth,int ct)
  • author = auth; (std::string has assignment operators)
  • ob1.store("Tolkin",10); (single quotes are for char-literals)
  • Give card::show() a body. You currently have just the declaration. And because show() does not mutate card, make it a const member function: void show() const;

The real fix (sounds lapidar, but is my serious, well-intentioned advice):

Community
  • 1
  • 1
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

Not really, no. For any given instance of card, author is stored at a specific, fixed, memory location relative to the rest of the object. (author is actually inside the object: if you do std::cout << sizeof(card) << std::endl; you'll see that card is 40 bytes, because all it contains is an array of forty characters.) &author = ... is trying to tell the compiler that author is now stored somewhere else, but there's no way to tell the compiler that, because you already promised that author is stored inside the card.

That said, you could change your declaration of author to be a true pointer, rather than an array:

char * author;

and then assign to it like so :

author = auth;

but that's not a good idea. Whenever you pass a pointer around, you have to keep track of where it's gone, to make sure that all of your pointers are always valid, and that you never lose a pointer to memory you need to de-allocate later.

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

I think the basic misunderstanding here is the way that C/C++ handle "char arrays" as strings. The strcpy routine copies the contents of a string, where the = assignment operator (applied to char) copies a single character, or a pointer to the string. &author = *auth will look at the auth pointer, dereference it using *, and take the single char found there, then take the address of (&) your char[] named author, and try to change the address to the char value.

You could…

  • Use strcpy to copy the contents of the string (but, in new code, don't use strcpy, use strncpy instead!)
  • Store a pointer to the string provided (char* author in your class would be assigned as author = auth; but then, if auth is free()d or delete[]()ed later, you will have a pointer to memory that no longer contains your string, which is bad)
  • Use a C++ std::string object instead of a C-style char[] for your string, which will behave more like a PHP string would. std::string author could be copied from std::string auth using author = auth.

String-handling in C++ is a big subject, but you will definitely want to get a good understanding of the differences between "thing" and "pointer-to-thing" types … !

Also, in C++, you must use "" around strings, and '' around single chars. There is a lot less "magic" in a C/C++ "" string, though; only \x type escape sequences work (for example, there is no "$var" substitution available). In Perl/PHP/Bourne/... you use '' for non-escaped strings and "" for escaped strings; in C++, since char and char[]/std::string are different types, they use the punctuation differently.

BRPocock
  • 13,638
  • 3
  • 31
  • 50
  • Thanks, that was the kind of answer i needed.I'll look more closely to the things you have pointed out in your message. – Leron Dec 05 '11 at 17:08
  • Moving from a more dynamic language like PHP to C++, (speaking as someone who works in Lisp and Perl for "fun" and C++ for "the day job"), pointers are the *constant* "pebble in the shoe." Two things to keep in mind, whenever you see a pointer, are: "Who is responsible for `malloc`/`free` / `new`/`delete` this pointer?" and "am I sure this input can never exceed my string/array size?" (assume it always will…) Functions like `strncpy` and `snprintf` are more-or-less mandatory. (On Linux/Unix, the `man`ual pages for `strcpy` and its ilk will also include `strncpy`.) – BRPocock Dec 05 '11 at 17:12
-1

you are mixing up some types: PHP is not typed while c++ is.

you can t compile because you are trying to assign a pointer to a reference.

&author = *auth;

i suggest to read a lot of documentation about reference and pointers!! cheers!

andrea.marangoni
  • 1,499
  • 8
  • 22
  • what you should do is : authors = auth; but i am quite sure it is not a good way to do the things. – andrea.marangoni Dec 05 '11 at 16:57
  • Not to mention, using single quotes to pass a string, you need to use double quotes – Dan F Dec 05 '11 at 16:57
  • Wrong. Neither &author nor *auth are a reference - & only makes a reference out of something if it's next to a **type** name, not a variable name. In that case it would deliver the address of author (but that is an rvalue and can't be assigned). – codeling Dec 05 '11 at 16:59
  • @riskio: The assignment won't work out, he stores a char-array, not a pointer – Sebastian Mach Dec 05 '11 at 16:59
  • @riskio: I'm not sure that's even valid syntax, yes `authors` by itself is technically a pointer, I don't think the compiler will let you reassign the address of an array allocated on the stack like that – Dan F Dec 05 '11 at 17:00
  • @phresnel a constant array as char array[10] can be seen as a constant pointer. in fact i can pass array[10] to a function as char*. if you are talking about single quote, you are right.. – andrea.marangoni Dec 05 '11 at 17:12
  • @riskio: `&author = *auth;` makes no sense in the C++ world. Pointers are not arrays. Writing into string-literals is undefined behaviour. C++ has no array assignments, you can `std::copy` with correct iterators, or better, use a standard-container in the first place. – Sebastian Mach Dec 06 '11 at 10:39
  • @riskio: You write `i suggest to read a lot of documentation about reference and pointers!! cheers!`. I suggest to you the same. – Sebastian Mach Dec 06 '11 at 10:39
  • in fact is the first thing that i wrote!(&authors = *auth). – andrea.marangoni Dec 06 '11 at 10:53
  • in fact is the first thing that i wrote!(&authors = *auth can t compile). actually i see now and i m sorry i wrote wrong saying that &author is a reference..ops.. the sense was that the assignment was wrong.. – andrea.marangoni Dec 06 '11 at 11:02
  • @riskio: If I wouln't have had this tab open, I would not have recognized your comment (so always write @) ;) Can you not edit your post to state that this is invalid code? – Sebastian Mach Dec 06 '11 at 16:26