Thread: Error C2109 + C2228

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    18

    Error C2109 + C2228

    Okay so I have a global structure defined as...

    Code:
    struct branch { /*branches*/
        char trait[385];
        int counter[386];
    } b[64];
    I have this structure defined and use it in several programs with similar goals. All of the programs work except for one that is more complicated into which I put an extra function that allows me to shorten the length of the code. The function is...

    Code:
    void ev(int a, int b, int c) {    
        int i;
        for(i=0; i<a; i++) {
            b[b].trait[i]=b[c].trait[i];
            b[b].counter[i]=b[c].counter[i];
        }
        b[b].counter[a]=b[c].counter[a];
        for(i=a; i<385; i++) bsim(i, b);
    }
    When I try to run the build the program it says that on the 4th, 5th, and 7th lines of that code above have errors C2109 and C2228. I don't understand what is wrong with them because when I use almost identical formats in other parts of this program and others it works just fine. Any help with how to fix the problem would be appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    We're not Microsoft compilers, we're people. We don't have Microsoft's compiler error codes committed to memory. How about copying and pasting all the errors exactly as they appear?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    1>Yule64-1.cpp(915): error C2109: subscript requires array or pointer type
    1>Yule64-1.cpp(915): error C2228: left of '.trait' must have class/struct/union
    1>Yule64-1.cpp(915): error C2109: subscript requires array or pointer type
    1>Yule64-1.cpp(915): error C2228: left of '.trait' must have class/struct/union
    1>Yule64-1.cpp(916): error C2109: subscript requires array or pointer type
    1>Yule64-1.cpp(916): error C2228: left of '.counter' must have class/struct/union
    1>Yule64-1.cpp(916): error C2109: subscript requires array or pointer type
    1>Yule64-1.cpp(916): error C2228: left of '.counter' must have class/struct/union
    1>Yule64-1.cpp(918): error C2109: subscript requires array or pointer type
    1>Yule64-1.cpp(918): error C2228: left of '.counter' must have class/struct/union
    1>Yule64-1.cpp(918): error C2109: subscript requires array or pointer type
    1>Yule64-1.cpp(918): error C2228: left of '.counter' must have class/struct/union

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    void ev(int a, int b, int c) {
    Code:
    b[b].trait[i]=b[c].trait[i];
    Yup, b is neither a pointer nor an array, nor the struct you want to treat is as.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps the real question is why you're giving a global variable such a stupid and meaningless name as 'b'.

    Especially when you're using the same for parameter names.

    Introduce some naming convention so the two never collide, say all globals are CamelCaseWithFirstLetterCapitalised and all locals are just lowercase.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rags_to_riches View Post
    We're not Microsoft compilers, we're people. We don't have Microsoft's compiler error codes committed to memory. How about copying and pasting all the errors exactly as they appear?
    Speak for yourself! Just kidding.

    As for the OP maybe if you didn't name the whole variable set b, bbb ,bbbbbb and other b names the compiler could actually tell if you are referring to some poorly typedefed struct or to your equally ambigous function parameter.

    EDIT: I meant to say struct declaration, just saw now that you didn't actually typedef anything.
    Last edited by claudiu; 04-23-2012 at 10:46 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    It's all fixed now thanks. I just changed the input b to d. The other b (the one it the structure) is just short for the word branch btw.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    *facepalm*
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by deeisenberg View Post
    It's all fixed now thanks. I just changed the input b to d. The other b (the one it the structure) is just short for the word branch btw.
    That's not fixed. That's masking the problem. Like Salem said, use proper variable names and you wouldn't have had this problem in the first place.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  2. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  3. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  4. Compiler error c2228
    By <showMe> in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 08:31 PM
  5. Replies: 4
    Last Post: 11-09-2002, 01:16 PM