这个C ++的东西很新,并且一直在尝试整夜用子文件夹中的头文件编译一个基本程序 - 我必须是一个非常愚蠢但很容易修复的东西。也许我对名称空间缺乏了解 - 或者如何包含标题???
希望有人可以帮助我
所有示例似乎都说我可以在一行或单独的步骤中编译/链接 - 单独的步骤似乎给我的错误数量最少
我可以获取cpp文件来编译和创建.o文件,但无法创建输出文件。
最后一次尝试: g++ ProcessCAP.o ./network/SocketClient.o -o ProcessCAP
错误讯息:
./network/SocketClient.o:(.rodata+0xc0): undefined reference to 'CAP::SocketClient::send(std::string)'
collect2: error: ld returned 1 exit status
主程序:
#include <iostream>
#include <signal.h>
#include "network/SocketClient.h"
using namespace std;
using namespace CAP;
//int argc, char *argv[]
int main(void){
cout << "Starting CAP Processor Example" << "\n";
SocketClient sc("streaming1.my-url.com", 8080);
sc.connectToServer();
string rec = sc.receive(4096);
while (rec.length() > 0) {
cout << "Received [" << rec << "] \n";
string rec = sc.receive(4096);
}
sc.disconnectFromServer();
cout << "End of CAP Processor Example" << "\n";
}
&#34;网络中的文件&#34;子文件夹:
SocketClient.cpp
#include "SocketClient.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;
namespace CAP {
SocketClient::SocketClient(std::string serverName, int portNumber) {
this->socketfd = -1;
this->server = NULL;
this->serverName = serverName;
this->portNumber = portNumber;
this->isConnected = false;
}
int SocketClient::connectToServer(){
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd < 0){
perror("Socket Client: error opening socket.\n");
return 1;
}
server = gethostbyname(serverName.data());
if (server == NULL) {
perror("Socket Client: error - no such host.\n");
return 1;
}
bzero((char *) &serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serverAddress.sin_addr.s_addr, server->h_length);
serverAddress.sin_port = htons(portNumber);
if (connect(socketfd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0){
perror("Socket Client: error connecting to the server.\n");
return 1;
}
this->isConnected = true;
return 0;
}
string SocketClient::receive(int size=4096){
char readBuffer[size];
int n = read(this->socketfd, readBuffer, sizeof(readBuffer));
if (n < 0){
perror("Socket Client: error reading from socket");
}
return string(readBuffer);
}
int SocketClient::disconnectFromServer(){
this->isConnected = false;
close(this->socketfd);
return 0;
}
SocketClient::~SocketClient() {
if (this->isConnected == true){
disconnectFromServer();
}
}
} /* namespace CAP */
SocketClient.h (也在网络子文件夹中)
#ifndef SOCKETCLIENT_H_
#define SOCKETCLIENT_H_
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
namespace CAP {
class SocketClient {
private:
int socketfd;
struct sockaddr_in serverAddress;
struct hostent *server;
std::string serverName;
int portNumber;
bool isConnected;
public:
SocketClient(std::string serverName, int portNumber);
virtual int connectToServer();
virtual int disconnectFromServer();
virtual int send(std::string message);
virtual std::string receive(int size);
bool isClientConnected() { return this->isConnected; }
virtual ~SocketClient();
};
}
#endif /* SOCKETCLIENT_H_ */