0

I am currently learning C++ and trying to understand the usage of structs.

in C++. As far as I'm aware, if you want to define a function after the main() function, you have to declare it beforehand, like in this function (Please tell me if I'm wrong with it):

#include "stdafx.h"
#include <iostream>
#include <string>

void printText(std::string); // <-- DECLARATION

int main()
{
    std::string text = "This text gets printed.";
    printText(text);
}

void printText(std::string text)
{
    std::cout << text << std::endl;
}

My question now is if there is a way to do the same with structs. I don't want having to always define a struct before the main() function, just because I prefer it like that. However, I get an error when I try doing it like that:

//THIS program DOESN'T work.    
#include "stdafx.h"
#include <iostream>
#include <string>

struct Products {std::string}; // <-- MY declaration which DOESN'T work

int main()
{
    Products products;
    products.product = "Apple";
    std::cout << products.product << std::endl;
}

struct Products
{
    std::string product;
};

When I delete the decleration and instead define the struct before the main function, the program works so I assume I'm somehow wrong with the decleration:

//THIS program DOES work
#include "stdafx.h"
#include <iostream>
#include <string>

struct Products
{
    std::string product;
};

int main()
{
    Products products;
    products.product = "Apple";
    std::cout << products.product << std::endl;
}

Could someone tell me if there is some way to declare a struct like that? Bear with me if I have any major mistake in the code, I'm just a beginner. Thanks in advance!

4
  • 1
    struct Name; is a forward-declaration you're looking for. Commented Oct 5, 2016 at 18:56
  • you should post the exact error message along with the question Commented Oct 5, 2016 at 18:59
  • How is this offtopic? Commented Oct 5, 2016 at 19:00
  • 5
    A forward declaration won't work here, as main() uses members from the struct. You have to define the struct before main. If you dislike the aesthetics, put the struct in a header. Commented Oct 5, 2016 at 19:01

2 Answers 2

11

You can pre-declare (forward-declare) a class type in C++.

struct Products;

However, a class type declared in this way is incomplete. Incomplete types can only be used in a number of very limited ways. You will be able to declare pointers or references to such type, you will be able to mention it in non-defining function declarations etc., but you will not be able to define objects of such incomplete type or access their members.

If you want to define objects of class Products or access members of class Products, you have no other choice but to fully define the class before such use.

In your case you are defining an object of type Products in main as well as accessing members of class Products there. This means that you have to completely define Products before main.

Sign up to request clarification or add additional context in comments.

9 Comments

@AnT "you have to completely define" I think declaration is enough.
@πάνταῥεῖ : I'm not sure what you mean. The OP's usage requires class definition to be visible from main (i.e. a defining declaration is needed, as opposed to a non-defining one).
@AnT The ole confusion about declaration and definition. A forward declaration isn't a full class declaration.
@πάνταῥεῖ With all due respect, you seem to be the one who is confused about the accepted terminology.
@TimSeguine Be assured I'm not.
|
1

In your particular case a forward declaration wont help, because a forward declaration only allows you to use pointers or references, as e.g. in

struct foo;
foo* bar(foo f*) { return f;}
struct foo { int x; }

However,

struct Products {std::string};

is not a declaration, but if you want an ill-formed declaration and definition. The correct forward declaration would be:

struct Products;

2 Comments

I didn't read very carefully to be honest, but I didn't downvote either. I would suggest to switch first and second part of an answer because first one is kind of irrelevant to the given question.
@Andrey good point. actually I added the last sentence only later. As I understand the question it is about forward declaring a struct in general and OPs code just serves as an example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.