struct in another .cpp file

closed account (jLNv0pDG)
So, I have a big list of struct objects that I don't want to put in main.cpp but I want to be able to use them in it.

Is there an easy way of creating all these structure in struct.cpp, for example, and then including struct.cpp in main.cpp?
You need to declare the struct in a header file ( and if it has some complex functions define them in a separate source file )
http://www.cplusplus.com/forum/articles/10627/
closed account (jLNv0pDG)
Thanks for the link, I think I understand this a little better now but I have come across a problem and I can't see why this doesn't work:

1
2
3
4
//fruit.h
struct fruit {
string name;
}


1
2
3
4
5
6
//main.cpp
#include <string>
#include "fruit.h"

int main()
{ ... }


I keep getting "syntax error : missing ';' before identifier 'name'". Even putting #include<string> in the fruit.h makes no difference. What am I doing wrong?

---

EDIT: I realise that should have been std::string
Last edited on
Missing a semicolon on line 4 of fruit.h.
closed account (jLNv0pDG)
Has C++ given anyone else a passionate hate for the semi-colon? :)
Heavens no!
After a while you just get accustomed to where you should put the semicolons.
It is annoying until you remember.
(And it's a fashionable, useful punctuation mark but nobody cares about that.)
I forget to put ';' after my classes/structs every so often too...<_<;
Topic archived. No new replies allowed.