.h, .cpp and main.cpp files

I have a problem with these files. I dont know what im doing wrong.
Here is example:
1
2
3
4
5
6
7
   //(mod.h)
        #ifndef MOD_H
        #define MOD_H
        namespace M {
        int fun (int);
        }
        #endif 


1
2
3
4
5
6
7
8
9
10
//(mod.cpp)
        #include<iostream>
        #include "mod.h"
        namespace M {
        int fun(int x){
        x=x*x;
        return x;
        }
                     }
        #endif  



1
2
3
4
5
6
7
8
  //(main.cpp)
        #include<mod.h>
        using namespace std;
        int main(){
        int x=10;
        cout<<fun(x)<<endl;
        return 0;
        } 


Can anyone tell where is mistakes? :(
Last edited on
#include "mod.h" (with quotes instead of angle brackets).

then either

cout << M::fun( x ) << endl;

or

using namespace M;

or

using M::fun;

Ok now it looks like this:

----------------------------------------------------
1
2
3
4
5
6
7
//(mod.h)
#ifndef MOD_H
#define MOD_H
namespace M {
      int fun (int);
      }
#endif 


----------------------------------------------------

1
2
3
4
5
6
7
8
9
//(mod.cpp)
#include "mod.h"
namespace M {
     int fun (int x){
     x=x*x;
     return x;
     }
                    }
#endif 


----------------------------------------------------
1
2
3
4
5
6
7
8
9
10
//(mainmod.cpp)
#include<iostream>
#include<mod.h>
using namespace std;
using namespace M;
int main(){
  int a=10;
  cout<<fun(a)<<endl;
  return 0;
  }

----------------------------------------------------

So i put these 3 files in one directory, than change directory
to that and compile it writing: g++ mainmod.cpp mod.cpp -o prgmod


and there are these errors:

mainmod.cpp:2:16: error: mod.h: No such file or directory
mainmod.cpp:3: error: ‘M’ is not a namespace-name
mainmod.cpp:3: error: expected namespace-name before ‘;’ token
mainmod.cpp: In function ‘int main()’:
mainmod.cpp:8: error: ‘fun’ was not declared in this scope
mod.cpp:11:2: error: #endif without #if


Can anyone help me?
Last edited on
#include<mod.h>
As jsmith told you, use quotes with your own headers: #include "mod.h"
In the code you provided you are using 'mod' instead of 'M'

At the end of mod.cpp you have an extra #endif

and please use [code][/code] tags when posting code
Last edited on
Really tnx you guys, everything is working :)
Last edited on
Topic archived. No new replies allowed.