Problems with struct

Hi,

I have a problem when I try to use a struct in my program. The program returns Segmentation Fault even now when I just declare a vector of it.

What I have is a class and in the header file i declare my struct among other things (dont know where else to put it)

1
2
3
4
5
6
7
struct data
{
	string name;
	int score;
};

vector<data> mHighscoreData;


This is the only place where I use my struct, just declaring a vector of my struct (in the header aswell) and my program returns segmentation fault. When I comment out //vector<data> mHighscoreData , the segmentation fault dissapper. Does anyone know what I'm doing wrong?

Thanks
Last edited on
just declaring a vector of my struct (in the header aswell)
To have global variables declared in headers, you should use extern and define them in a cpp file
Header:
extern vector<data> mHighscoreData;
Cpp:
vector<data> mHighscoreData;

This avoids multiple definitions of the same symbol
When I try doing that I get an error saying:


../CLI.h:51: error: storage class specified for ‘mHighscoreData’
What's your code now?
In the header file I declare the struct and type
 
extern vector<data> mHighscoreData;

and in the constructor in the .cpp
 
vector<data> mHighscoreData;
It should work, could you post lines near these?
My header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef CLI_H_
#define CLI_H_

#include "Die.h"
#include <string>
#include <vector>
#include <iostream>
#include <limits>
#include <fstream>
using namespace std;

/**
* Denna klassen sköter all interaktion med användaren,
* skriver ut och läser in. CLI = Command-line Interface
* @author Christian Andersson
*/

class CLI {

	public:

		CLI(); //Konstruktor
		CLI(const CLI& o); //Kopierings konstruktor
		~CLI(); //Destruktor

		CLI& operator=(CLI& o); //Tilldelningsoperatorn
		void printDice(vector<Die>&); //Ritar tärningarna
		int getInt(int lowLimit=0, int highLimit=0); //Hämtar en int med möjlighet att bestämma den nedra och den övre gränsen
		string getString(); //Hämtar en string
		void printMenu(); //Ritar ut menyn
		void printString(string); //Ritar ut en sträng
		void printInt(int); //Ritar ut ett tal
		void printScoreBoard(vector<int>&,int,int,int); //Skriver ut poängen'
		void printHighscore(); //Skriver ut highscoren

	protected:
	private:
		void initResultsBoard(); //Initierar resultat vektorn

		struct data
		{
			string name;
			int score;
		};

		string mTextTop;
		string mTextBody1, mTextBody2, mTextBody3;
		string mTextBottom;
		vector<string> mDisplayResults;
		extern vector<data> mHighscoreData;



};

#endif


And my constructor in .cpp
1
2
3
4
CLI::CLI() //Konstruktor
{
	vector<data> mHighscoreData;
}
Last edited on
I said you should use extern for global variables, not for class members. Remove it and the declaration in the constructor
Your problem is somewhere else
ok, I misunderstood. But the problem remains, when I just have
 
vector<data> mHighscoreData;


In my class it returns segmentation fault and when I comment it, no problem. So where else could the problem be?
Are you using CLI objects somewhere?
Yes, one place else. Declaring an object of CLI as a class member in another class header then this class use this to communicate to the user.
Does the segmentation fault occur when that object gets constructed or when you are accessing some methods?
Are you initializing mHighscoreData in the constructor or using it in some function?
Last edited on
The segmentation fault comes when I exit the program. I dont use mHighscoreData anywhere else for now. just declaring it in CLI.h
A declaration shouldn't cause segmentation fault.
If the CLI destructor gets called when you exit the program you should look there.
My CLI destructor is empty
Try to see if the error is related to the other vector you have in the class: mDisplayResults
I don't know what else could be
Topic archived. No new replies allowed.