hello have simple program main.cpp
a.h
, a.cpp
. i'd define class in a.cpp , call on method of class in main.cpp
my a.h
:
#include <iostream> using namespace std; class hello { string hello(); };
my a.cpp
#include "a.h" #include <iostream> string class hello::hello(){return "hello world";};
my main.cpp
#include "a.h" int main() { hello h; cout << h.hello(); }
edit : changed include"a.cpp"
a.h
, added string definition of method hello. added #include <string>
a.h
while compiling error
"a.cpp:4:22: error: ‘hello’ in ‘class hello’ not name type string class hello::hello() {return "hello";};"
"a.cpp:4:28: error: expected unqualified-id before ‘)’ token string class hello::hello() {return "hello";};"
a simple problem, have declared function part of class:
class hello { // ^function part of class hello. string hello(); // ^function returns string // ^function called "hello" // ^function takes no arguments.
so when go define function need give compiler same information:
string hello::hello() { // ^function returns string // ^function part of class hello // ^function called "hello" // ^function takes no arguments.
- you need add header
<string>
file facilitate use ofstring
object. - your
'#include "a.cpp"
needs#include "a.h"
, rule of thumb should never ever see#include file.c/cpp
. - finally, need make function
hello
public allow use outside class members.
but best advice can give, pick c++ beginners book. world of good. the definitive c++ book guide , list
Comments
Post a Comment