Vector with custom classes

Hello all! I'm new here, so I'll start of by thanking you all for a great and helpful community! Many of my former problems have been resolved all thanks to you. But now I'm at a halt and so I post my problem here in hope to get some expert help. I've been struggling with this for ~2 hours now and scouring the internet have yielded no results.

I'm still new to C++ so the concept of pointers and references are still confusing to me and I'm afraid that's where my code breaks apart.

Basically, what I'm trying to do is accessing an instance of a custom class from a vector, but all I'm getting is this error:

‘m’ is not a class or namespace

Main.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "Movie.cpp"
using namespace std;

// Definiera övriga funktioner
void readFromDatabase();
void displayMovies(int);
void chooseDisplayMode();
//void

// För att inte skicka tillbaka användaren
// till huvudmenyn vid felinmatning i submenyer
// använder vi en valideringsflagga
bool validinput;
// Innehåller våra sparade filmer
vector<Movie*> movies;
// Användarens menyval
int input;

int main ()
{
	readFromDatabase();
	
	while(true)
	{
		// Skriv ut vår otroligt snygga meny
		cout << endl << "+- Movie Manager 3000 ---+" << endl;
		cout << "1 :: Registrera film" << endl;
		cout << "2 :: Visa filmlista" << endl;
		cout << "3 :: Sok i lista" << endl;
		cout << "0 :: Avsluta" << endl << "> ";
		
		// Hantera oväntad inmatning
		if(!(cin >> input))
		{
			cin.clear();
			while(cin.get() != '\n')
				cout << "Endast siffror.\n\n";
				continue;
		}
		
		// Hantera val innanför vårt menyområde
		if(input >= 0 && input < 4)
		{
			switch(input)
			{
				case 1:
					break;
				case 2:
					chooseDisplayMode();
					break;
				case 3:
					break;
				case 0:
					exit(0);
					break;
			}
		}
		
	}	// End main while
}

// Läser in filmer från sparade databasen till filmvektorn
void readFromDatabase ()
{
	// Öppna filmdatabas
	ifstream dbfile ("db.txt");
	// Om filen existerar, läs innehållet till vår filmvektor
	if(	dbfile.is_open() )
	{
		// Databasrad
		string line;
		// Filminfo
		string title;
		int type;
		
		// Läs in varje rad
		// så länge där finns några kvar att läsa in
		while( getline(dbfile, line) )
		{
			// Bryt stränger vid : och mata in i vår vektor
			stringstream linestream(line);
			getline(linestream, title, ':');
			linestream >> type;
			
			movies.push_back(new Movie(title, type));
		}
	}
}

// Låter användaren välja vilket sätt att visa filmlistan på
void chooseDisplayMode ()
{
	validinput = false;
	
	while(!validinput)
	{
		cout << endl << "Visa lista enligt" << endl;
		cout << "1 :: Titel" << endl;
		cout << "2 :: Typ" << endl << "> ";
		
		// Hantera oväntad inmatning
		if(!(cin >> input))
		{
			cin.clear();
			while(cin.get() != '\n')
				cout << "Endast siffror.\n\n";
				continue;
		}
		
		// Ligger valet innanför det valbara området?
		if(input == 1 || input == 2)
			validinput = true;
	}
	
	displayMovies(input);
}

// Visar filmlista beroende på vilket sätt användaren valt
void displayMovies ( int i )
{
	// Håller den film vi arbetar med för stunden
	Movie* m;
	// Separation, struktur, överskådlighet
	cout << endl;
	
	if(i == 1)	// Visa enligt titel
	{
		for(unsigned int i = 0; i < movies.size(); i++)
		{
			m = movies[i];
			cout << m::title << "\t\t" << endl; // ERROR HERE
		}
	}
	else if(i == 2)	// Visa enligt typ
	{
	
	}
}


Movie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>
using namespace std;

class Movie
{
	public:
		Movie(string, int);
		static const int DVD;
		static const int BRAY;
		string title;
		string type;
};

const int Movie::DVD = 0;
const int Movie::BRAY = 1;

Movie::Movie ( string _title, int _type )
{
	title = _title;
	type = _type;
}
You don't use a scope resolution operator with an instance of an object. You use it with a class or namespace like the error says.

You did correctly on line 15 and 16 in Movie.cpp. If you want to get at m's title just use m.title;
Last edited on
The variable m is a pointer to a Movie object, so use m->title. -> is the the pointer to member operator.

The dot notation is for objects as in:

1
2
Movie MyMovie;
MyMovie.Title = "The Life of Pi"


I always prepend pointer variables with a "p" as in pMovie - that way you know it is a pointer variable, it might save you one day.

Some other naming conventions:

1. Member variables start with m_ and Capitalise words as in m_Title or m_MyVariable.
2. Classes start with C as in CMovie

These conventions help avoid problems because the type / use of the variable is more obvious.

The member variable convention makes it easier to name parameters /arguments for function calls. Now you can use Title as an argument because the member variable is m_Title.

Hope all goes well
Thanks TheIdeaMan! That resolved my problem!
Also thanks for the tip on naming conventions, I think I'll stick to your suggestion and use them from now on ;)
Topic archived. No new replies allowed.