i'm using g++
on debian 8.2 jessie.
i'm learning classes in c++. think understand basics, not how instantiate class objects 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 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
, got 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: @ global scope: movie.cpp:10:6: error: prototype ‘void movie::setmovieinfo(const string&, int, const string&)’ not match in class ‘movie’ void movie::setmovieinfo(const std::string &title, const int releaseyear, const std::string &description) ^ in file included 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); ^
reference parameters functions can little confusing. while define function with
void foo(int& i){}
when call function, not need pass reference. call
foo(x);
rather
foo(&x);
it can confusing. in case, call
movie bladerunner(title, releaseyear, description);
also, it's worth noting in code there no reason have parameters constructor reference parameters. reduce memory usage little, shouldn't concern when you're first trying code working. movie(std::string title, int releaseyear, std::string description)
fine.
Comments
Post a Comment