Where is my error in this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
class Write{
     public:
             Write(char*);
      void* ret(){return a;}
       private:
      char *a;
      };
      Write::Write(char * b){
            a=b;
            }
            
int main(){
    Write *c;
    c->Write("Hello");
    cout<<c->ret()<<endl;
    system("pause");
return 0;    }
Last edited on
closed account (zb0S216C)
I would point the finger at line 17, the std::cout statement. You're trying to print the contents of a void pointer, without casting. As a result, std::cout doesn't know what type it's dealing with.

I would also say that there're some important flaws within your code that you need to address, such as the assignment of C-strings (within Write's constructor). For such an operation, consider std::strcpy( ) or even better, use std::string.

Why is C-string assignment bad?
Because pointer a is now pointing to the memory which b is pointing to. This is bad since a could delete the string again, or the other way around. Instead, a should be assigned an entire copy of b's contents.

Wazzak
Last edited on
Topic archived. No new replies allowed.