using namespaces to access classes that are in separate files

Let's say you have a main() file, 8 separate cpp files, and 8 corresponding header files (that represent classes, of course) Is it possible to assign each cpp file a different namespace, say namespace na for class a, and then namespace nb for class b, and so on?
Sure, why not?
would you then just make instances of the class, and the functions to be called, inside the namespaces?
No, you can still create instances of the class outside the namespace, just the class and function declarations would have to be in the namespace.
What I am trying to do is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <sstream>
#include <new>
#include <math.h>
#include <time.h>
#define g 9.8
#include "include/intro.h"
#include "include/Labone.h"

using namespace std;
Labone e [5];     //Getting ready to make 5 instances of the class Labone
//Setting all elements of the array to instances...
for (int n = 0; n<5;n++)
{
    e[n] = Labone(n);
}   //end initializer
namespace na
{
    e[0].etitlea();
    for (int nu = 0; nu<5;nu++)
    {
        e[nu].adata(nu);
    }   //end data handler a
    e[0].etitleb();
    for (int num = 0; num<5; num++)
    {
        e[num].getaccel();
        e[num].bdata(num);
        e[num].setaccel();
    }   //end data handler b
    e[0].analysis();
}   //end namespace

int main()
{
    int x;
    string mystr;
    cout << "Welcome to the Physics Lab Program!"<<endl;
    cout<<'\n'<<"The labs for this course that there are programs for are as follows: "<<endl;
    cout<<'\n'<<"1 - 1D Kinematics"<<endl;
    cout<<"3 - Newton's Laws"<<endl;
    cout<<"4 - Friction"<<endl;
    cout<<"5 - Work and Energy"<<endl;
    cout<<"6 - Collisions"<<endl;
    cout<<"7 - Torque"<<endl;
    cout<<"8 - Simple Harmonic Motion"<<endl;
    cout<<"9 - Fluids"<<endl;
    cout<<'\n'<<"Please make your selection: ";
    getline(cin,mystr);
    stringstream(mystr)>>x;
    return 0;
}  //end main 


I am trying to call the class Labone (and its functions) how I normally would if this were one file, when the user inputs 1.
Last edited on
You know... you have a using namespace std; there right? What do you think that does? I think you should read something about namespaces somewhere.
I know that's the case, and I know that is the standard namespace, which is associated with entities created with iostream. I am trying to use different namespaces to handle any problems with coincidence of variable names. I am trying to do something like what is shown here, but with classes: http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
Just put your class definitions from your headers inside a namespace.

1
2
3
4
5
6
7
namespace physX
{
    class Whatever
    {
        //define class as normal
    };
}


Then class Whatever would be accessed as physX::Whatever in other parts of the code, unless you have using namespace physX; somewhere.
It doesn't generate any compile errors! At least none concerning this. It is, however, generating errors as to the empty files I have, and simply "complains all the way down" after I remove them from the project. Why is this? Better yet, never mind. I have decided not to use different namespaces for different classes. The variables are all private (last time I checked), so there is no need!
Last edited on
Maybe you forgot a bracket somewhere? Or don't have include guards on your header files. Its hard to say what the problem is without any code.
I am now trying to put them all into one big program, which has been done for the most part (on every program except the final version of the one I was creating in this thread; this one isn't included because I am trying to make its data depend less on intro(), a common class for all the other programs. I am trying to do this while maintaining structure.) Here is my .cpp file (sorry for posting the entire file):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <fstream>
#include <sstream>
#include <new>
#include <math.h>
#include <time.h>
#define g 9.8
#include "include/intro.h"
#include "include/Labone.h"
#include "include/Labthree.h"
#include "include/Labfour.h"
#include "include/Labfive.h"
#include "include/Labsix.h"
#include "include/Labseven.h"
#include "include/Labeight.h"
#include "include/Labnine.h"

using namespace std;

int main()
{

    int x;
    string mystr;
    cout << "Welcome to the Physics Lab Program!"<<endl;
    cout<<'\n'<<"The labs for this course that there are programs for are as follows: "<<endl;
    cout<<'\n'<<"1 - 1D Kinematics"<<endl;
    cout<<"3 - Newton's Laws"<<endl;
    cout<<"4 - Friction"<<endl;
    cout<<"5 - Work and Energy"<<endl;
    cout<<"6 - Collisions"<<endl;
    cout<<"7 - Torque"<<endl;
    cout<<"8 - Simple Harmonic Motion"<<endl;
    cout<<"9 - Fluids"<<endl;
    cout<<'\n'<<"Please make your selection: ";
    getline(cin,mystr);
    stringstream(mystr)>>x;

    //intro i;
    intro i(x);
    i.names();

    if (x==1)
    {
        Labone o [5];     //Getting ready to make 5 instances of the class Labone
        //Setting all elements of the array to instances...
        for (int n = 0; n<5;n++)
        {
            o[n] = Labone(n);
        }   //end initializer
        o[0].etitlea();
        for (int nu = 0; nu<5;nu++)
        {
            o[nu].adata(nu);
        }   //end data handler a
        o[0].etitleb();
        for (int num = 0; num<5; num++)
        {
            o[num].getaccel();
            o[num].bdata(num);
            o[num].setaccel();
        }   //end data handler b
        o[0].analysis();
    }   //end if
    else if(x==3)
    {
        Labthree e (1);
        e.titleone(1);
        e.tabledata();
        Labthree exp (2);
        exp.titleone(2);
        exp.tabledata();
        exp.analysis();
    }   //end case 3
    else if(x==4)
    {
        Labfour f;
        f.reminder();
        f.partone();
        f.parttwo();
        f.printtables();
        f.partthree();
        f.analysis();
    }   //end case 4
    else if (x==5)
    {
        Labfive w;
        w.partone();
        w.parttwo();
        w.partthree();
        w.analysis();
    }   //end case 5
    else if (x==6)
    {
        string dum;
        float d,g;
        cout<<"Enter the mass of cart 1 (in kilograms): ";
        getline(cin,dum);
        stringstream(dum)>>d;
        cout<<"Enter the mass of cart 2 (in kilograms): ";
        getline(cin,dum);
        stringstream(dum)>>g;
        Labsix ex[6];
        for (int entry = 0;entry<6;entry++)
        {
            //calling everything...
            ex[entry] = Labsix(entry,d,g);
            ex[entry].udothis();
            ex[entry].arraysetter();
        }   //end for
        ex[0].printstart();
        for (int turn = 0; turn<6;turn++)
        {
            ex[turn].printdataa();
        }   //end for
        ex[1].printstart();
        for (int row = 0;row<6;row++)
        {
            ex[row].printdatab();
        }   //end for
        ex[2].printstart();
        for (int rowtwo = 0;rowtwo<6;rowtwo++)
        {
            ex[rowtwo].printdatac();
        }   //end for
        ex[0].analysis();
    }   //end case 6
    else if (x==7)
    {

    }   //end case 7
    else if (x==8)
    {
        Labeight l[10];
        //initializing all instances...
        for (int val= 0;val<10;val++)
        {
            l[val] = Labeight(val);
        }   //end for
        l[0].ttitle();
        for (int v = 0; v<10;v++)
        {
            l[v].tabledata();
        }   //end for
        l[0].analysisone();
        l[0].xlstitle();
        for (int va = 0; va<10;va++)
        {
            l[va].newrows();
        }   //end for
        l[0].analysistwo();
        l[0].ecalc();
    }   //end case 8
    else if (x==9)
    {
        Labnine n;
        n.partone();
        n.parttwo();
        n.analysis();
    }   //end case 9
    else
    {

    }   //end else
    return 0;
}   //end main 
It's giving me errors like :
line 96:Expected unqualified-id before numeric constant; line 102: no match for operator>> 

I think my error stems from the fact that I don't know how to properly make and use a makefile (I am using Code::Blocks and running Windows).
Last edited on
Topic archived. No new replies allowed.