this pointer

closed account (1vf9z8AR)
I want the output to display the name and salary of person.I must use 'this' pointer.

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
#include<iostream>
#include<string.h>
using namespace std;
class salesman
{
private:
        char name[80];
        int pay;
public:
        salesman(char *str,int *p)
        {
                strcpy(name,str);
                pay=p;
        }
        void outdata()
        {
                cout.write(this->name,80);
                cout<<" has salary ";
                cout.write(this->pay);
        }
};

int main()
{
        salesman s1("Raman",4000),s2("Rita",2000);
        s1.outdata();
        s2.outdata();
}
Last edited on
And? What problem are you having?

You seem to have mistaken this for the telepath forum.

EDIT: I suggest you might find this tutorial helpful:

http://www.cplusplus.com/doc/tutorial/basic_io/

Also, at line 13, I'm sure that's not doing what you want to do. Hint: pay and p are different types.

Last edited on
closed account (1vf9z8AR)
@MikeyBoy
oh sorry lol.

These are the errors i am getting.

||=== Build: Debug in thispointer (compiler: GNU GCC Compiler) ===|
F:\c++projects\thispointer\pointerthis.cpp||In constructor 'salesman::salesman(char*, int*)':|
F:\c++projects\thispointer\pointerthis.cpp|13|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
F:\c++projects\thispointer\pointerthis.cpp||In member function 'void salesman::outdata()':|
F:\c++projects\thispointer\pointerthis.cpp|19|error: no matching function for call to 'std::basic_ostream<char>::write(int&)'|
F:\c++projects\thispointer\pointerthis.cpp|19|note: candidate is:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\ostream.tcc|182|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::streamsize = int]|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\ostream.tcc|182|note: candidate expects 2 arguments, 1 provided|
F:\c++projects\thispointer\pointerthis.cpp||In function 'int main()':|
F:\c++projects\thispointer\pointerthis.cpp|24|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
F:\c++projects\thispointer\pointerthis.cpp|24|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
F:\c++projects\thispointer\pointerthis.cpp|10|note: initializing argument 2 of 'salesman::salesman(char*, int*)'|
F:\c++projects\thispointer\pointerthis.cpp|24|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
F:\c++projects\thispointer\pointerthis.cpp|24|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
F:\c++projects\thispointer\pointerthis.cpp|10|note: initializing argument 2 of 'salesman::salesman(char*, int*)'|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
closed account (1vf9z8AR)
These are the errors i am getting.

||=== Build: Debug in thispointer (compiler: GNU GCC Compiler) ===|
F:\c++projects\thispointer\pointerthis.cpp||In constructor 'salesman::salesman(char*, int*)':|
F:\c++projects\thispointer\pointerthis.cpp|13|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
F:\c++projects\thispointer\pointerthis.cpp||In member function 'void salesman::outdata()':|
F:\c++projects\thispointer\pointerthis.cpp|19|error: no matching function for call to 'std::basic_ostream<char>::write(int&)'|
F:\c++projects\thispointer\pointerthis.cpp|19|note: candidate is:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\ostream.tcc|182|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::streamsize = int]|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\ostream.tcc|182|note: candidate expects 2 arguments, 1 provided|
F:\c++projects\thispointer\pointerthis.cpp||In function 'int main()':|
F:\c++projects\thispointer\pointerthis.cpp|24|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
F:\c++projects\thispointer\pointerthis.cpp|24|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
F:\c++projects\thispointer\pointerthis.cpp|10|note: initializing argument 2 of 'salesman::salesman(char*, int*)'|
F:\c++projects\thispointer\pointerthis.cpp|24|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
F:\c++projects\thispointer\pointerthis.cpp|24|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
F:\c++projects\thispointer\pointerthis.cpp|10|note: initializing argument 2 of 'salesman::salesman(char*, int*)'|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
Strangely enough, your compiler is telling you exactly what I told you.
closed account (1vf9z8AR)
name and *str are not different types??

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
#include<iostream>
#include<string.h>
using namespace std;
class salesman
{
private:
        char name[80];
        int *pay;
public:
        salesman(char *str,int *p)
        {
                strcpy(name,str);
                pay=p;
        }
        void outdata()
        {
                cout.write(this->name,80);
                cout<<" has salary ";
                cout.write(this->pay);
        }
};
int main()
{
        salesman s1("Raman",4000),s2("Rita",2000);
        s1.outdata();
        s2.outdata();
}
Nobody is talking about name and str. What MikeyBoy is talking about is p and pay which he has clearly put in bold so I don't know why are you bringing name and *str up?

And the fact of the matter is they are differrent types.
pay is an int
p is a pointer to int

This clearly indicated here:
F:\c++projects\thispointer\pointerthis.cpp|13|error: invalid conversion from 'int*' to 'int' [-fpermissive]

And you have multiple such errors too.
Hint: You actually don't want p to be a pointer at all.

I also suggest the OP to stop treating compilation errors as
my code doesn't work
and instead view them as useful hints on where things might have gone wrong.
closed account (1vf9z8AR)
i thought if pay and *p are different then so must be name and *str.
closed account (1vf9z8AR)
For this
cout.write(this->*pay);
i get this
F:\c++projects\thispointer\pointerthis.cpp|19|error: '((salesman*)this)->salesman::pay' cannot be used as a member pointer, since it is of type 'int*'|

how do i get around this one?I tried float but similar error comes
i thought if pay and *p are different then so must be name and *str.


But pay and *p are the same type.

pay and p are different types.

I have a feeling that the real problem is that you don't really understand pointers, nor the syntax for declaring and using them. I'd recommend going back to your textbook, and getting a clearer understanding of that chapter.
closed account (1vf9z8AR)
my textbook is really bad for pointers so are the online resources(too advanced).Do you know any study material online for just the basics of pointers?Unfortunately i am not able to devote time to computer science because of my main subjects physics chemistry maths.
Do you know any study material online for just the basics of pointers?

Where do you think you are?

http://www.cplusplus.com/doc/tutorial/pointers/

This tutorial was written for somebody who has never heard about pointers in their life.
If this is too advanced for you then you're just not trying hard enough.
closed account (1vf9z8AR)
ooooooh thanks. I didnt know the meaning of tutorial so i didnt notice it.
Topic archived. No new replies allowed.