I have the next piece of code:

vector<ChatSession> ssid;

void SessionCreator( void *ch ) 
{
	vector<string> users;

       ...
       ...
       ...

	ChatSession cs;

	// put chat session objects in vector array
	for (int i = 0; i < (int)users.size(); i++)
	{
		const char *t = users[i].data();
		char t2[30]; memcpy(t2, t, strlen(t));
		cs = new ChatSession(*t2);
		ssid.push_back(cs);
	}

	// Terminate myself
	_endthread();
}

program.h:

class ChatSession
	{
	private:
		char ChatID[100];
		char Name[30];
		char Message[1024];
		char newMsg[1024];
		bool CanSend;
	public:
		ChatSession();
		ChatSession(char *nickname);
		~ChatSession();
		void SetChatID(char* id);
		void Send();
		char *getName();
		bool ready();
	};
	...
	...
	...

extern vector<ChatSession> ssid;

VC++ throws angry error error at me: Line: cs = new ChatSession(*t2); Error C2664: 'ChatSession::ChatSession(char *)' : cannot convert parameter 1 from 'char' to 'char *' Please help.

Recommended Answers

All 7 Replies

>>cs = new ChatSession(*t2);

The error message is telling you that *t2 is just a single character, not a character array. Maybe you mean this? cs = new ChatSession(t2);

So, I think I've figured something out for you, I tried building it myself and I can think of two different ways to fix the error:

for (int i = 0; i < (int)users.size(); i++)
	{
		const char *t = users[i].data();
		char t2[30]; memcpy(t2, t, strlen(t));
		ChatSession cs(t2);
		ssid.push_back(cs);
	}

and the other way, using a pointer:

ChatSession* cs;

	// put chat session objects in vector array
	for (int i = 0; i < (int)users.size(); i++)
	{
		const char *t = users[i].data();
		char t2[30]; memcpy(t2, t, strlen(t));
		cs = new ChatSession(t2);
		ssid.push_back(*cs);
	}

Hope that helps!

I modified my code to use char pointer vector, but this still doesn't work.. it gives another error now:

void SessionCreator( void *ch ) 
{
	vector<char *> users;

	// Split the string
	Split((char *)ch, ", ", users);

	ChatSession cs;

	// Initialize the ssesions array with each user
	for (int i = 0; i < (int)users.size(); i++)
	{
		cs = new ChatSession(users[i]);
		ssid.push_back(cs);
	}

	// Terminate myself
	_endthread();
}

Line: cs = new ChatSession(users[i]); error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ChatSession *' (or there is no acceptable conversion)

>>cs = new ChatSession(*t2);
The error message is telling you that *t2 is just a single character, not a character array. Maybe you mean this? cs = new ChatSession(t2);

Gives me the same error as above.

>>ChatSession cs;

cs must be a pointer -- ChatSession* cs;

>>ChatSession cs;

cs must be a pointer -- ChatSession* cs;

That worked, thanks.
But can someone explain why do i have to declare it as a pointer?

You don't need to declare it as a pointer, although the pointer definitely works.

See my previous post and you can create a normal instance as well. With this non-pointer method you're just creating that object every time and initializing it with t2 (or whatever variable) and it should work the same as using a pointer.

Let me know if it doesn't.

That worked, thanks.
But can someone explain why do i have to declare it as a pointer?

You had to declare it as a pointer because on line 13 you used the new operator to allocate memory for it. If you delete line 13 then you don't need a pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.