-2

I have a class A implemented by files A.hpp and and A.cpp.
One of the methods of class A receives a message, translates it and stores it into a structure.
The method signature looks something like this and the method is public:

eRetCode A::ParseInfo(sometype* pMessage, tParsedInfoFromA& ParsedInfo);

In my opinion tParsedInfoFromA type should be defined (using typedef) at A.hpp since it is relevant to class and meaningless without the class.
The simplest way is to define it above the class.
However, I feel that file A.hpp should begin with declaration of class A.
So I would like the typedef of the structure to appear after class declaration.

Does C++ provide me with legitimate way (not some "ugly" trick) to indicate that tParsedDataFromA is defined below so I can use a reference to structure of type tParsedInfoFromA at declaration of ParseInfo method?
I tried forward declaration but the compiler won't have it.

Would appreciate your comments

AndyG
  • 39,700
  • 8
  • 109
  • 143
Victoriia
  • 105
  • 9
  • What do you mean by _the compiler won't have it_? – BlackDwarf Jul 10 '15 at 12:59
  • Like the phrase ' a picture is worth a thousand words' a small code example would be fine for your question ;) – mbed_dev Jul 10 '15 at 13:01
  • Where is `tParsedInfoFromA` declared? – NathanOliver Jul 10 '15 at 13:01
  • *"However, I feel that file A.hpp should begin with declaration of class A."* - Why? – Christian Hackl Jul 10 '15 at 13:01
  • IMHO You don't save anything by typedef'ing `tParsedInfoFromA`. In fact, it's probably useful that the user knows the type they'll need to pass in, since the method is public. – AndyG Jul 10 '15 at 13:03
  • @DeepBlackDwarf I receive the following errors: `forward declaration of 'struct tParsedInfoFromA'`, `declaration of 'const struct tParsedInfoFromA'`, `struct tParsedInfoFromA has a previous declaration as struct tParsedInfoFromA` – Victoriia Jul 10 '15 at 14:18
  • @mbed_dev I agree. Next time I will add it. This time dureuill already described it at his answer. Please see my comment to his answer – Victoriia Jul 10 '15 at 14:23
  • @Christian Hackl Personal opinion. When I open A.hpp usually I want to see the declaration of class A. I don't want to search for it. – Victoriia Jul 10 '15 at 14:24
  • @NathanOliver The typedef appear after class declaration at A.hpp. Also please see my comment to the answer I received from dureuill – Victoriia Jul 10 '15 at 14:25
  • @AndyG I typedef so I won't need to write the word struct. It doesn't prevent from user the knowledge that it is a struct. However, your comment was very useful since I did some search and found the following: [link](http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c) – Victoriia Jul 10 '15 at 14:27
  • @Victoriia: You're writing in C++, not C. You can simply refer to a struct by its name now. – AndyG Jul 10 '15 at 18:20
  • @AndyG First of all, I agree. My first week at C++ after a long break during which I coded C. So old habits take time to die. Hope reading some relevant material and consulting here regards best practice will help me to write proper, clean C++. In addition, as you can see from the link I added to my other comments, it is not always that simple, although I agree that most of the time typedef is unnecessary. – Victoriia Jul 12 '15 at 11:35

1 Answers1

0

Assuming your class A has no member, method return or parameter of type tParsedInfoFromA (only tParsedInfoFromA& or tParsedInfoFromA*), and you provide the implementation for class A in the cpp file, you can achieve what you want.

In your header, forward declare you struct as:

struct tParsedInfoFromA;

Then, define class A:

class A 
{
    eRetCode A::ParseInfo(sometype* pMessage, tParsedInfoFromA& ParsedInfo);
    // ...
};

Then, define your struct:

struct tParsedInfoFromA {
    // ...
};

Some thoughts, though. If tParsedInfoFromA (not the best name but I assume the actual name is better) consists of only a few members, it makes sense to have it defined before A. If it is longer, you'd better have it in a separate file. Lastly, if you feel that the definition for your struct is undissociable of your class, you can declare the struct nested in A.

class A
{
    struct tParsedInfoFromA
    {
      // struct members
    }
    eRetCode A::ParseInfo(sometype* pMessage, tParsedInfoFromA& ParsedInfo);
    // class members
};
dureuill
  • 2,526
  • 17
  • 24
  • @dureuillHi, dureuill Thank you so much for the answer. Previously I was doing the same thing you suggested with one difference - I used typedef. With typedef it gives errors. Without typedef it works ok. As a result of the comments I did a search and came with this [link] (http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c). According to this typedef is not necessary (except for hiding issues), but still I am curious to understand why with typedef I have errors. – Victoriia Jul 10 '15 at 14:34
  • @dureuillThe errors are: `forward declaration of 'struct tParsedInfoFromA'`, `declaration of 'const struct tParsedInfoFromA'`, `struct tParsedInfoFromA has a previous declaration as struct tParsedInfoFromA` – Victoriia Jul 10 '15 at 14:34
  • @dureuillRegards the thoughts: I have multiple classes similar to A, each with its own knowledge, all inherit from same father, all have ParseInfo methods. Some structs are short but some long. I don't like the idea of putting it before class declaration. The idea of putting it in separated file will result in multiple files, each contains only the struct. User also will need to include both class hpp and additional h file. The idea of declaring the structure in the class is interesting. I need to read more about it to understand whether it works for me. Thanks again! – Victoriia Jul 10 '15 at 14:39