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.

Dani AI

Generated

Short answer: the compiler error came from mixing object and pointer types. As noted, new returns a pointer; assigning that pointer to a non‑pointer local ChatSession is a type mismatch. As pointed out, you can instead construct a local ChatSession and push it into the vector.

Practical improvements and pitfalls to avoid:

  • Prefer value semantics: make ChatSession hold std::string members and accept const std::string& in the constructor. Then construct temporaries and let the vector store copies or moves. This avoids manual new and the lifetime/ownership headaches that follow.

    Example (modern style): construct in place with emplace_back so no extra temporaries are needed.

  • If you do store pointers in the container, manage ownership explicitly. Use std::unique_ptr<ChatSession> (or shared_ptr if shared ownership is required). That prevents leaks and makes intent clear.

  • Be careful with C-style buffers: copying into a fixed buffer without ensuring a terminating NUL leads to undefined behaviour. Prefer std::string or, if you must copy, use safe functions and respect buffer sizes.

  • Copy/move semantics: std::vector<T> copies or moves elements. If ChatSession manages raw resources (C strings, raw pointers), implement proper copy/move constructors and assignment operators, or switch to RAII types like std::string.

  • Threading: SessionCreator looks like a thread function. Concurrently calling push_back on a shared std::vector without synchronization is undefined. Protect the vector with a mutex or use a thread-safe queue/collection. See std::vector thread-safety notes: std::vector — Thread safety.

Applying those changes will remove the type error and make the code safer and easier to maintain.

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.