ofxTCPClient cannot be put in vector?

i need to setup several ofxTCPClient in one app, so i want to use an vector to store them. the code is:

ofApp.h

#pragma once
#include "ofMain.h"
#include "ofxNetwork.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

		std::vector<ofxTCPClient> tcpClients_;
};

ofApp.cpp
#include “ofApp.h”

void ofApp::setup(){
    ofxTCPClient tcpClient;
	tcpClient.setup("127.0.0.1", 8000);
	tcpClient.setMessageDelimiter("\n");
	tcpClients_.push_back(tcpClient);
}

however, there are compile errors say: error C2248: “ofxTCPManager::ofxTCPManager”: unable to visit private member(declared in “ofxTCPManager”)

Hi yangyangcv,

This is because ofxTCPClient is using ofThread. It it unsafe to store ofxTCPClient instances in a std::vector because of the way a vector moves things in memory. Instead you can store a pointer to an instance in the vector.

When I try your code XCode gives me this message “Call to implicitly-deleted copy constructor of ‘ofxTCPClient’”

This would be my suggestion:

class ofApp : public ofBaseApp{
  
public:
	void setup();
    //....
  
	std::vector<ofxTCPClient*> tcpClients_;
};

void ofApp::setup(){
  ofxTCPClient *tcpClient = new ofxTCPClient();
  tcpClient->setup("127.0.0.1", 8000);
  tcpClient->setMessageDelimiter("\n");
  tcpClients_.push_back(tcpClient);
}

//========================================================================
int main() {
	ofSetupOpenGL(1024,768,OF_WINDOW);
	ofRunApp(new ofApp());
}

fantastic! it works!