Problem with Classes and Objects

Hey guys, i was learning classes and objects from a tutorial on youtube. I was learning, how to use variable of a private access specifier. But on the compilation, i got flooded with errors.
following are the errors (after compiling it on dev c++ 4.6.92[ i know its a bad compiler])
_____________________________________________________________________________
Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Users\tanmay\Desktop\c++\Untitled1.cpp" -o "C:\Users\tanmay\Desktop\c++\Untitled1.exe"   -g3  -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" -g3 
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
                 from C:\Users\tanmay\Desktop\c++\Untitled1.cpp:1:

C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
C:\Users\tanmay\Desktop\c++\Untitled1.cpp:21: error: new types may not be defined in a return type

C:\Users\tanmay\Desktop\c++\Untitled1.cpp:21: error: extraneous `int' ignored
C:\Users\tanmay\Desktop\c++\Untitled1.cpp:21: error: `main' must return `int'
C:\Users\tanmay\Desktop\c++\Untitled1.cpp: In function `int main(...)':
C:\Users\tanmay\Desktop\c++\Untitled1.cpp:24: error: no match for 'operator<<' in 'std::cout << bo.tanmay::getname'
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:63: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:74: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:86: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:121: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:155: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:98: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:178: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]

C:/Dev-Cpp/include/c++/3.4.2/ostream:189: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:193: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:179: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:214: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:238: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:219: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:261: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:284: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:307: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:449: note:                 std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]

C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:505: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:460: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:465: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:567: note:                 std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:612: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:499: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:504: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]

Execution terminated

____________________________________________________________________________

and the code i ran is:

__________________________________________________________________________

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
[code]#include<iostream.h>
#include<conio.h>
#include<string.h>
using namespace std;
class tanmay
{
      public:
             void setname(int a)
             {
                  ab=a;                 
                  }
                  int getname()
                  {
                      return ab;
                      }
      private:
              int ab;
      }
      int main()
      {
          tanmay bo;
          bo.setname(234);
          cout<<bo.getname;
          getch();
          return 0;
      
}
[/code]
____________________________________________________________________

please tell me where am i wrong. (sorry for such a long post)
Remove
getch();
When writing a class make sure the ending bracket has a semi colon after it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class tanmay
{
      public:
             void setname(int a)
             {
                  ab=a;                 
                  }
                  int getname()
                  {
                      return ab;
                      }
      private:
              int ab;
};

That should fix it
Also, you are not calling getname correctly.

It should be
 
cout<<bo.getname();
Also, remove the .h from iostream and string.
closed account (3qX21hU5)
class tanmay
1
2
3
4
5
6
7
8
9
10
11
12
13
{
      public:
             void setname(int a)
             {
                  ab=a;                 
                  }
                  int getname()
                  {
                      return ab;
                      }
      private:
              int ab;
};


I'm sorry but that is some of the weirdest indentation I have seen, whats up with the closing braces? ;p

Also another tip would be to update your IDE/Compilers you are still using Mingw/GCC 3.4.2 which is highly outdated and has many bugs in it and missing a lot of features. It is also missing a lot of errors. Since you like Dev C++ I would recommend checking out Orwell's Dev C++ which is has just recently been updated. Here is a link to their download http://sourceforge.net/projects/orwelldevcpp/
Last edited on
Topic archived. No new replies allowed.