how do you make a class if you have a 3 files, sample.h which has a class type, and then sample.cpp which has all the functions, somehow it works when i put into a one file but doesn't when i try to compile together with 3 files.
for example: lets say i have def.h class def{ void something() } then i have a def.cpp: void def::something() bla bla bla.... but it doesn't work
thats pretty much what i have and it works using one file: http://liveworkspace.org/code/c898c672ff8f77414b93334e1b7ae71c but when i separate it into 3 files, from line 1 to 16 make a stack.h and from line 17 to 37 to stack.cpp and the rest in main.cpp it gives me a lot of errors
the names of the files must be identical, i.e add.h, add.cpp e.t.c
it is...
im sure ur code has a slight mistake just recheck it again,
In your def.cpp, did you #include "def.h" ?
yes i did
Did you also include them in your main.cpp?
yes
Did you use #ifndef in the header file to be sure not to redefine your class? Because outside of some more explicit details I'm afraid it is really just a guessing game at this point.
Post the three files, the error messages and the compile command.
You need to call 'void something()' as public. Because a class automatically makes it private if you haven't defined it to public. Here is an example: class def{ public: void something() } And then you will be able to call "void def::something()"
i have assigned it to public
thats my make file: run: stack.o main.o g++ $(FLAGS) -o run stack.o main.o stack.o: stack.cpp stack.h g++ $(FLAGS) -c stack.cpp main.o: main.cpp stack.h g++ $(FLAGS) -c main.cpp clean: rm -f run *.o
Mind posting the error message, so we at least know what is not working? ^^
a lot of things
g++ -c stack.cpp In file included from stack.cpp:2: stack.h:11: error: expected initializer before ‘*’ token stack.h:14: error: ‘Stack’ has not been declared stack.h:15: error: ‘Stack’ has not been declared stack.h:16: error: ‘Stack’ has not been declared stack.h:17: error: ‘Stack’ has not been declared stack.cpp:3: error: variable or field ‘push’ declared void stack.cpp:3: error: ‘Stack’ was not declared in this scope stack.cpp:3: error: ‘pstack’ was not declared in this scope stack.cpp:3: error: expected primary-expression before ‘x’ stack.cpp:11: error: ‘std::string Stack2::top’ is not a static member of ‘class Stack2’ stack.cpp:11: error: ‘Stack’ was not declared in this scope stack.cpp:11: error: ‘stack’ was not declared in this scope stack.cpp:11: error: expected ‘,’ or ‘;’ before ‘{’ token stack.cpp:14: error: ‘bool Stack2::is_empty’ is not a static member of ‘class Stack2’ stack.cpp:14: error: ‘Stack’ was not declared in this scope stack.cpp:14: error: expected ‘,’ or ‘;’ before ‘{’ token stack.cpp:17: error: variable or field ‘pop’ declared void stack.cpp:17: error: ‘Stack’ was not declared in this scope stack.cpp:17: error: ‘stack’ was not declared in this scope make: *** [stack.o] Error 1
i don't know why i get so much errors when it works correctly in one file
Can you give us the codes? So we can see what you did wrong
one sec
//file: stack.h #include <iostream> using namespace std; #ifndef STACK_H #define STACK_H struct Node { string x; Node *next; } typedef Node* Stack; class Stack2{ public: void push(Stack* pstack, string); void pop(Stack& stack); string top(Stack& stack); bool is_empty(Stack stack); }; #endif
//file: stack.cpp #include "stack.h" void Stack2::push(Stack* pstack, string x){ Node* t; t=new Node; t->x=x; t->next=*pstack; *pstack=t; return; } string Stack2::top(Stack& stack){ return stack->x; } bool Stack2::is_empty(Stack stack){ stack= NULL; } void Stack2::pop(Stack& stack){ Node* p; p=new Node; p=stack->next; stack=p; return; } //file: main.cpp #include <iostream> #include "stack.h" #include <vector> #include <string> using namespace std; int main(){ size_t i; Stack2 Stack2; Stack stack = 0; string arr[]= {"1","2","3","4"}; vector<string> x(arr, arr +4); cout<<"Original string vector \n"; for (i=0; i<x.size(); i++){ cout<<x[i]<<", ";} cout<<endl; for (i=0; i<x.size(); i++){ Stack2.push(&stack,x[i]);} cout<<"After using the stack to hold the string in reverse order we get: \n"\ ; Node* p; p=stack; while (p !=0){ cout<<p->x<<", "; p=p->next; } cout<<endl; cout<<"After we use top() we get: \n"; cout<<Stack2.top(stack)<<endl; cout<<"After we use pop() we get: \n"; Stack2.pop(stack); Node* w; w=stack; while (w !=0){ cout<<w->x<<", "; w=w->next; } cout<<endl; }
Why do you have what appears to be a handle, typedef after the struct? typedef Node* Stack;
where should i put it then?
When programming in C I usually do it like this: typedef struct Node { string x; Node *next; // not sure you can create a new object inside the definition } Stack_t;
i don't think thats the problem, because why does it work in one file?
that still doesn't work
waittt it works
That is where the first error in your log is pointing, error: expected initializer before ‘*’ token The second is pointing here: void pop(Stack& stack); saying that stack is undefined.
i just put ; after } struct Node { string x; Node *next; }
Well good, one down, 20 to go. :)
that actually fixed everything :) lol
Excellent
Indeed
now i have to put this into a template class
lol
Good luck.
Just a question tho, why are you using both class and struct? Isn't it easier to just use one instead of both? ^^ And also, put the ifndef and define at the very top of the file, and then everything else. so it looks like this: #ifndef _STACK_H_ #define _STACK_H_ #include <iostream> using namespace std;
can we use class to define node or what?
how would you change this into a template as im really confused now, I know we should declare template<class W> but what then I can change to W
Join our real-time social learning platform and learn together with your friends!