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:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "Movie.cpp"
usingnamespace 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(unsignedint i = 0; i < movies.size(); i++)
{
m = movies[i];
cout << m::title << "\t\t" << endl; // ERROR HERE
}
}
elseif(i == 2) // Visa enligt typ
{
}
}
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.
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 ;)