LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2011, 12:14 AM   #1
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 366
Blog Entries: 1

Rep: Reputation: 44
GCC Error: ‘foo’ has no member named ‘bar’


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
Attached Files
File Type: txt cards.c.txt (3.3 KB, 25 views)
File Type: txt cards.h.txt (568 Bytes, 23 views)
File Type: txt war.c.txt (2.9 KB, 15 views)

Last edited by jrtayloriv; 04-30-2011 at 12:28 AM.
 
Old 04-30-2011, 12:25 AM   #2
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 366

Original Poster
Blog Entries: 1

Rep: Reputation: 44
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.

Last edited by jrtayloriv; 04-30-2011 at 12:28 AM.
 
Old 04-30-2011, 02:06 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by jrtayloriv View Post
Bah -- been looking at this for 30 minutes and, naturally, right after I posted, ...
Doesn't sound unfamiliar
 
  


Reply

Tags
c-language, error, gcc, struct



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is foo bar satimis Programming 25 04-26-2019 08:21 PM
grep -v for foo or bar Viper Chief Linux - General 2 05-07-2008 02:52 AM
BASH get marked RegEx result: "foo s bar" | /foo(.*)bar/ 1 -> " s " hansschmucker Programming 6 10-23-2007 10:34 PM
foo@bar:~$ <- how to change bar? JussiKp Linux - Newbie 4 08-03-2006 05:46 AM
For foo in bar; do angel115 Programming 1 11-16-2005 04:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration