strcpy(struct.arrayofchars[], char*)

strcpy(struct.arrayofchars[], char*)

Post by hira » Wed, 06 Nov 2002 18:06:33



hi

I've got a struct of the following:
-----------------------------------------------
struct Word {
    Word() {letters[0] = '\0';}
    Word(const char *in) {strcpy(letters, in);}
    char letters[64];

Quote:};

int main()
{

   Word w();
   char buffer[64] = "abcdefghijkl"

    strcpy(w.letters, buffer);
     cout << w.letters << endl;

Quote:}

---------------------------------

using g++, I get errors (request for member `letters' in `w', which is
of non-aggregate type `Word ()()'

However, if i change everything to :

int main()
{

    char w[64] = {0};
   char buffer[64] = "abcdefghijkl"

    strcpy(w, buffer);
     cout << w << endl;

Quote:}

everything works out just fine.

Anybody know why I can't use a char array in a struct the way same as
just a normal char array?  Thanks for any help
hiram

 
 
 

strcpy(struct.arrayofchars[], char*)

Post by Andrey Tarasevic » Wed, 06 Nov 2002 18:15:01



> hi

> I've got a struct of the following:
> -----------------------------------------------
> struct Word {
>     Word() {letters[0] = '\0';}
>     Word(const char *in) {strcpy(letters, in);}
>     char letters[64];
> };

> int main()
> {

>    Word w();
>    char buffer[64] = "abcdefghijkl"

>     strcpy(w.letters, buffer);
>      cout << w.letters << endl;
> }

> ---------------------------------

> using g++, I get errors (request for member `letters' in `w', which is
> of non-aggregate type `Word ()()'
> ...

  Word w();

is a function declaration, not a defintion of an object 'w'. That's why you
get that error message. Loose '()'

  Word w;

and everyting will compile.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

 
 
 

strcpy(struct.arrayofchars[], char*)

Post by Ruslan Abdikee » Wed, 06 Nov 2002 18:36:55



Quote:> int main()
> {
>    Word w();

This is a declaration of function "w" which returns Word.
Hence an error message.

Remove parentheses, and everything will work just fine.

      Word w;

Hope it helps,
Ruslan Abdikeev

 
 
 

strcpy(struct.arrayofchars[], char*)

Post by hira » Thu, 07 Nov 2002 12:09:07


Okay, thanks guys for the tip.  Took your advice and everything worked
out just fine.  However, I thought that this was how we initialized
objects in C++.  The only thing I can figure is the compiler just does
some checking of types: eg

int myInt(8);  /initializes a new integer with a value of 8 == (int
myInt = 8)
int my2Int();  //compiler thinks that this is a function that takes no
parameter
                      //and returns an int

my2Int = 12;       //this is illegal because we are trying to assign
an int to a (supposed)
                           //function

So, basically if I have some sort of object with maybe a few different
constructors, if I want a new object I'd do something like:

MyintObject i(15);
MyfloatObject f(23.6);

but anytime I want to initalize a new object using the default (empty)
constructor, I must
lose the parentheses so the compiler doesn't think it's a function
declaration.

MyObect O;       //good, make a MyObject called O using default
constructor;
MyObject T();     //bad, func definition

Hopefully I got this right in my head now. Thanks again for the help.
hiram

 
 
 

1. b.template to_string<char, char_traits<char>, allocator<char> >()

hello,

i was reading the bitset class chapter in "the c++ standard library: a
tutorial and reference" by Nicolai M. Josuttis, and came upon such
statements:

string bitset<bits>::to_string () const

[...snip..]

* this function is a template function that is parameterized only my the
return type. according to the language rules, you must write the foloowing:

bitset<50> b;
b.template to_string<char, char_traits<char>, allocator<char> >()

could someone explain to me what language rule this b.template to_string..
reffers to? i looked in the 'templates' chapter in stroustrup, but could not
find much. perhaps, that's because i dont know what i am looking for.

if the explanation is long and you dont have the time, please let me know
at least where i should look...

thanks very much.

denis

2. HP200lx vs. Psion 3a

3. (char **) versus (const char**) and (const char* const*)

4. Automation Object Error

5. struct, pointer and (unsigned char *) problem

6. is there any way to get a monitor for indy work station

7. casting char* into struct*

8. list of domain controller

9. Writing structs w/ char*'s to a file

10. How do I cast a Struct to a Char array?

11. fill struct[] into std::vector<const char*>

12. Moving address from hostent struct to in_addr struct?

13. Virtual destructor problem in structs (and if struct is a class)