I'm using g++
on Debian 8.2 Jessie.
I'm learning about classes in C++. I think I understand the basics, but not fully how to instantiate class objects with header files.
Here's Movie.h
:
#ifndef MOVIE_H
#define MOVIE_H
#include <string>
#include <iostream>
class Movie
{
private:
std::string m_title;
int m_releaseYear;
std::string m_description;
public:
Movie(std::string &title, int releaseYear, std::string &description);
~Movie()
{
std::cout << "\nDestructor called\n";
}
void setMovieInfo(std::string &title, int releaseYear, std::string &description);
std::string getTitle();
int getReleaseYear();
std::string getDescription();
void printInfo();
};
#endif
Then there's Movie.cpp
:
#include "Movie.h"
// Movie constructor
Movie::Movie(std::string &title, int releaseYear, std::string &description)
{
setMovieInfo(std::string &title, int releaseYear, std::string &description);
}
// Movie mem function
void Movie::setMovieInfo(const std::string &title, const int releaseYear, const std::string &description)
{
m_title= title;
m_releaseYear= releaseYear;
m_description= description;
}
std::string Movie::getTitle()
{
return m_title;
}
int Movie::getReleaseYear()
{
return m_releaseYear;
}
std::string Movie::getDescription()
{
return m_description;
}
void Movie::printInfo()
{
std::cout << "Title: " << m_title << '\n';
std::cout << "Year: " << m_releaseYear << '\n';
std::cout << "Description" << m_description << '\n';
}
And main.cpp
:
#include "Movie.h"
int main(){
std::string title;
int releaseYear;
std::string description;
title= "Blade Runner";
releaseYear= 1982;
description= "Harrison Ford's character hunts a group of runaway four-year-olds.";
Movie bladeRunner(title, releaseYear, description);
bladeRunner.printInfo();
return 0;
}
I ran g++ -Wall Movie.cpp main.cpp -o main.sh && ./main.sh
, but got this output:
Movie.cpp: In constructor ‘Movie::Movie(std::string&, int, std::string&)’:
Movie.cpp:6:27: error: expected primary-expression before ‘&’ token
setMovieInfo(std::string &title, int releaseYear, std::string &description);
^
Movie.cpp:6:35: error: expected primary-expression before ‘int’
setMovieInfo(std::string &title, int releaseYear, std::string &description);
^
Movie.cpp:6:64: error: expected primary-expression before ‘&’ token
setMovieInfo(std::string &title, int releaseYear, std::string &description);
^
Movie.cpp: At global scope:
Movie.cpp:10:6: error: prototype for ‘void Movie::setMovieInfo(const string&, int, const string&)’ does not match any in class ‘Movie’
void Movie::setMovieInfo(const std::string &title, const int releaseYear, const std::string &description)
^
In file included from Movie.cpp:1:0:
Movie.h:20:8: error: candidate is: void Movie::setMovieInfo(std::string&, int, std::string&)
void setMovieInfo(std::string &title, int releaseYear, std::string &description);
^