Problem with classes

May 13, 2013 at 5:26pm
I was trying to access the classes from different files. So , like a normal procedure, I made a new class, added the required codes and added the necessary preprocessor. But when i try to run the program, it says that my class has not been declared. I am not able to understand the cause. I am providing you the codes. Please help me out!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  //the main file.cpp....................

#include <iostream>
#include "sally.h"

using namespace std;

int main()
{
    sally a(23);
    sally b(87);
    sally c;
    c=a+b; // it gonna run operator+
    cout<<c.num;
    return 0;
}

____________________

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef SALLY_H
#define SALLY_H


class sally //my custom class, header file
{
    public:
        int num;
        sally();
        sally(int);
       sally operator+(sally);
};

#endif // SALLY_H 


____________________________

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "sally.h"  //the sally.cpp
#include<iosream.h>
using namespace std;
sally::sally()
{
  num=a;
}

sally sally :: operator+(sally aso){
sally Aobject;
Aobject.num=num+aso.num;
return (Aobject);
}


Thanks in advance!
May 13, 2013 at 5:28pm
What file is iosream.h, which is found in sally.cpp, line 2?
May 13, 2013 at 5:33pm
Did you make sure you added the files to your project file? This is different than the preprocessor directives. I know that for at least Code::Blocks, you have to both use #include and "add" files to the project.

Also there are four mistakes with your sally.cpp file.
1) The default constructor, sally(), makes use of a non-existent "a" variable.
2) There is no definition for the sally(int) constructor.
3) You misspelled iostream
4) You are using a deprecated iostream.h header. Just use iostream as you did in your main.cpp file.
Topic archived. No new replies allowed.