LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   GCC Error: ‘foo’ has no member named ‘bar’ (http://www.linuxquestions.org/questions/programming-9/gcc-error-%91foo%92-has-no-member-named-%91bar%92-877951/)

jrtayloriv 04-30-2011 01:14 AM

GCC Error: ‘foo’ has no member named ‘bar’
 
3 Attachment(s)
I'm doing a programming exercise from the book "Programming Challenges" where I'm trying to write a program that simulates the card game "War".

I'm getting the following errors when I try to compile my code:

Code:

cards.c: In function ‘shuffle’:
cards.c:54:39: error: expected expression before ‘Card’
cards.c:86:20: error: ‘Card’ has no member named ‘next’
cards.c:87:26: error: ‘Card’ has no member named ‘next’
cards.c:90:9: error: ‘Deck’ has no member named ‘top’
cards.c:91:9: error: ‘Deck’ has no member named ‘bottom’

...

cards.c: In function ‘new_standard_deck’:
cards.c:36:17: error: ‘Card’ has no member named ‘next’
cards.c:37:17: error: ‘Card’ has no member named ‘suit’
cards.c:38:17: error: ‘Card’ has no member named ‘rank’

...

(lots more of these)

I get this error every time I try to access the any of the members of either the Card or Deck structs, and I can't figure out why, because I have defined them as follows:

Code:

typedef struct Card {
    struct Card * next = NULL;
    unsigned short int rank = 0;
    unsigned short int suit = 0;
} Card;

typedef struct Deck {
    Card * top = NULL;
    Card * bottom = NULL;
} Deck;

I don't think it makes a difference, but I am compiling it with the following gcc command:

Code:

gcc -Wall -std=c99 war.c cards.c -o war
I'm sure there are plenty of other errors in my code, but for now, I'd just like someone to help me out with this one and let me try to figure out the rest on my own.

I've attached the full source files to this post, but it would only let me attach 3 of 4, so I posted the last header (war.h) to the bottom.

Thanks,
jrtayloriv

---------

Code:

//war.h

#ifndef WAR
#define WAR

#include <stdio.h>
#include "cards.h"

int war();
void init_war_decks(Deck * a, Deck * b);
int play_round(Deck * a, Deck * b);
double avg_game_length(int * turns_sample, int sample_size);

#endif


jrtayloriv 04-30-2011 01:25 AM

Bah -- been looking at this for 30 minutes and, naturally, right after I posted, I figured it out. I didn't realize that I couldn't set initial values within the struct definition.

i.e., just for anyone else who finds this through Google, you can't do:

Code:

struct foo {
    int a = 1; //assignment not allowed here
}

But you would have to do this instead:

Code:

struct foo {
    int a;
}

struct foo x;
x.a = 1;

Marking as solved.

Wim Sturkenboom 04-30-2011 03:06 AM

Quote:

Originally Posted by jrtayloriv (Post 4341590)
Bah -- been looking at this for 30 minutes and, naturally, right after I posted, ...

Doesn't sound unfamiliar :)


All times are GMT -5. The time now is 09:11 PM.