why doesnt this code work?

can some please help me figure out what is the problem?
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
#include <iostream>
#include <string>
using namespace std;

class Employee
{
	char* name;
	unsigned int salary;
public:
	Employee();
	Employee(char*,unsigned int);
	void print()const;
	~Employee();
};



Employee::Employee():salary(1000)
{
	name=new char[20];
}

Employee::Employee(char* name, unsigned int salary)
{
	name=new char[20];
	this->name=name;
	this->salary=salary;
}

void Employee::print()const
{
	cout<<"Name: "<<name<<endl;
	cout<<"Salary: "<<salary<<endl;
}

Employee::~Employee()
{
	delete [] name;
}
int main()
{
	Employee emp1("Bill Joones",5000), emp5("Jimmy Page");
Employee emp2=emp1;
Employee emp3;
emp3 = emp2;
Employee* workers = new Employee[3];
workers[0] = emp3;
workers[1] = Employee("Katy Larson");

delete [] workers;
 
}


those are the errors i get:

4 IntelliSense: no instance of constructor "Employee::Employee" matches the argument list
argument types are: (const char [12]) c:\Users\Al\Documents\Visual Studio 2012\Projects\ConsoleApplication33\ConsoleApplication33\main.cpp 48 14 ConsoleApplication33
3 IntelliSense: no instance of constructor "Employee::Employee" matches the argument list
argument types are: (const char [11]) c:\Users\Al\Documents\Visual Studio 2012\Projects\ConsoleApplication33\ConsoleApplication33\main.cpp 42 42 ConsoleApplication33
Error 1 error C2664: 'Employee::Employee(const Employee &)' : cannot convert parameter 1 from 'const char [11]' to 'const Employee &' c:\users\al\documents\visual studio 2012\projects\consoleapplication33\consoleapplication33\main.cpp 42 1 ConsoleApplication33
Error 2 error C2440: '<function-style-cast>' : cannot convert from 'const char [12]' to 'Employee' c:\users\al\documents\visual studio 2012\projects\consoleapplication33\consoleapplication33\main.cpp 48 1 ConsoleApplication33
Employee emp1("Bill Joones",5000), emp5("Jimmy Page");


With your emp5 object... you are attempting to pass a string to the constructor.

However none of your constructors take just a string. You have a constructor which does take a string, but it also requires a salary. So in order for that call to work, you either need to do one of 3 things:


1) Give Jimmy Page a salary, just like you're doing for Bill Joones.
or
2) Give the Employee constructor a default value for the salary so it becomes an optional parameter
or
3) Write a new constructor for Employee that takes only a single string.
i changed the consructor like you said:

1
2
3
4
5
6
Employee::Employee(char* name, unsigned int salary=1000)
{
	name=new char[20];
	this->name=name;
	this->salary=salary;
}

i dont get an arror but when i try to compile it there's a memory lick..can you tell me why?
Gah... I had a big reply and it got swallowed. Grrr.


Anyway...

You have an incomplete understanding of pointers and memory management. It would be much, much simpler for you to just use strings instead of trying to do the dynamic allocation yourself. I illustrate how to use strings in your other thread here:

http://www.cplusplus.com/forum/beginner/122432/#msg667187



Is there some reason you can't use strings? Like is this a school assignment or something?

If that's the case, you have quite a number of problems with what you're doing now:

#1) Your constructors allocate a new buffer for the name, but they do not initialize the buffer, so the employee's name will always be a bunch of random garbage characters.

#2) Your constructor effectively "throws away" any name that gets passed to it, by replacing the passed in pointer with a pointer to a dynamically allocated buffer.

#3) Since you have dynamic allocation, you need to overload the copy constructor and assignment operator. If you fail to do this, whenever an Employee is copied or reassigned, you'll have multiple objects owning the same buffer, and buffers will be deleted multiple times (bad -- possibly causing a program crash).



But again... all of these are non-issues if you just use strings.
Topic archived. No new replies allowed.