0

If I define a struct like below

typedef struct Coder {
    float **data;
};

For my convenience, I also define a pointer to this struct

typedef Coder *AUTO;

and then I have to initial it by calling

AUTO  myInstance = new Coder;

My problem appear when I call

myInstance->data= NULL;

The VC 2010 told me that there was no type specifier. I don't understand what problem is here. Could you please help me?

6
  • can you post error code? Commented Aug 20, 2013 at 11:28
  • @billz: "this declaration has no storage class or type specifier" Commented Aug 20, 2013 at 11:35
  • 1
    your typedef doesn't have a name. Also, what's convenient about the AUTO typedef? It just looks ugly and obfuscating to me. Commented Aug 20, 2013 at 11:44
  • 1
    @JonathanWakely The whole thing looks ugly and obfuscating. Why the typedef on the struct, to begin with? And while typedefing the pointer is already ugly and obfuscating, giving it a completely unrelated name, and all caps at that? Commented Aug 20, 2013 at 11:50
  • 1
    Instead of posting disjoint snippets, post a single code snippet that, when compiled, shows the error. As is, anyone reading this question has to guess at the parts that have been left out, so can't reproduce the problem. Commented Aug 20, 2013 at 11:56

3 Answers 3

6

If you are in C++, just do :

struct Coder
{
    float **data;
};

typedef Coder *AUTO;

Also you have to be sure that the AUTO declaration is done after the declaration of your struct or you can forward-declare your struct.


Also, it is possible that NULL is undefined.

You can replace it by 0 or just look at the link I just gave you.

Here is a live example.


EDIT : The code you gave us cannot work :

#include <tchar.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <iostream>

struct Coder {
        float **data; // 2-D matrix
};
typedef Coder *AUTO;

AUTO  myFeatures = new Coder;

myFeatures->data = NULL; // Forbidden

int main (){
        myFeatures->data = new float *[2];
        myFeatures->data[0] = new float [2];
}

You can only have declaration in namespace scope, not expression like that.

In §7.3.1/1 of the standard :

namespace-body:
     declaration-seqopt

which says the namespace-body can optionally contain only declaration.

This code will work :

// Your includes

struct Coder {
        Coder() : data(NULL) {} // Constructor who initialize data to NULL
                                // via the member-initialization-list

        float **data; // 2-D matrix
};
typedef Coder *AUTO;

AUTO  myFeatures = new Coder; // Do you want to keep it in global ??
                              // It's not a really good design

int main (){
        // You should put your declaration here.

        // myFeatures->data = NULL; useless now
        myFeatures->data = new float *[2];
        myFeatures->data[0] = new float [2];
}
Sign up to request clarification or add additional context in comments.

10 Comments

I'm guessing the myInstance = new Coder; that this is, in fact, C++ (the tag helps as well). And regardless, I don't believe that is the Op's issue.
Follow your comment, it doesn't work. The message VC2010 gave me "this declaration has no storage class or type specifier"
@user1432142 Can you show me the line where you see the error ? this example shows that this code is working.
The addendum about forward declarations makes this considerably closer to what is likely going on. The OP's structure declaration, while unusual, will work, and thus it alone is not the issue. Nowhere in the OP's code or question is there any evidence that struct Coder and the typedef for AUTO are even in the same file. Thus the reason for my answer.
@WhozCraig +1. I totally agree with you. I was thinking something like that.
|
1

In C typedef:

typedef int mango;

Now your mango represent "int" datatype.

user defined struct syntax:

struct mystruct;

use typedef:

typedef struct mystruct newtype;

Now newtype represent your struct mystruct.

Your the typedef name is missing,

typedef struct Coder {
    float **data;
}Coder ;

Comments

1
typedef struct Coder {
    float **data;
}Coder;

will be ok.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.