Problems with struct

Jul 16, 2009 at 11:49am
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 Jul 16, 2009 at 11:49am
Jul 16, 2009 at 11:53am
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
Jul 16, 2009 at 11:59am
When I try doing that I get an error saying:


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

and in the constructor in the .cpp
 
vector<data> mHighscoreData;
Jul 16, 2009 at 12:59pm
It should work, could you post lines near these?
Jul 16, 2009 at 1:03pm
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 Jul 16, 2009 at 1:03pm
Jul 16, 2009 at 1:15pm
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
Jul 16, 2009 at 1:46pm
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?
Jul 16, 2009 at 2:00pm
Are you using CLI objects somewhere?
Jul 16, 2009 at 2:07pm
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.
Jul 16, 2009 at 2:16pm
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 Jul 16, 2009 at 2:16pm
Jul 16, 2009 at 2:20pm
The segmentation fault comes when I exit the program. I dont use mHighscoreData anywhere else for now. just declaring it in CLI.h
Jul 16, 2009 at 2:27pm
A declaration shouldn't cause segmentation fault.
If the CLI destructor gets called when you exit the program you should look there.
Jul 16, 2009 at 2:39pm
My CLI destructor is empty
Jul 16, 2009 at 2:58pm
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.