Jak rozbić jeden plik .cpp na dwa?

0

witam, tak jak w tytule jak mogę to:

#include "stdafx.h"

#pragma comment(lib,"ws2_32.lib")

#include "definitions.h"

int connection() {
	//WinSock Startup
	WSAData wsaData;
	WORD DllVersion = MAKEWORD(2, 1);
	if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
	{
		MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
		return 0;
	}

	SOCKADDR_IN addr; //Address that we will bind our listening socket to
	int addrlen = sizeof(addr); //length of the address (required for accept call)
	addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Broadcast locally
	addr.sin_port = htons(7172); //Port
	addr.sin_family = AF_INET; //IPv4 Socket

	SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections
	bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket
	listen(sListen, SOMAXCONN); //Places sListen socket in a state in which it is listening for an incoming connection. Note:SOMAXCONN = Socket Oustanding Max Connections

	SOCKET newConnection; //Socket to hold the client's connection
	newConnection = accept(sListen, (SOCKADDR*)&addr, &addrlen); //Accept a new connection
	if (newConnection == 0) //If accepting the client connection failed
	{
		std::cout << "Failed to accept the client's connection." << std::endl;
	}
	else //If client connection properly accepted
	{
		std::cout << "Client Connected!" << std::endl;
		char MOTD[256] = "Welcome! This is the Message of the Day."; //Create buffer with message of the day
		send(newConnection, MOTD, sizeof(MOTD), NULL); //Send MOTD buffer
	}

	return 0;

}

void loading() {
#ifdef _WIN32
	SetConsoleTitle(STATUS_SERVER_NAME);
#endif

	std::cout << STATUS_SERVER_NAME << " - Version " << std::endl;
}

int main(int argc, char *argv[]){

	connection();
	loading();
	
	system("pause");
	return 0;
}



rozbić na dwa pliki cpp?
zalezy mi na tym zeby int connection przenieść np do connection.cpp

0

Stwórz projekt CPP
Dodaj drugi plik

i przenieś tam kod. który chcesz przenieść

w głownym pliku daj :

#include "mojNowy.cpp.cpp"

aby móc korzystać z zawartego tam kodu

1

Ehh takie proste, a nikt nie poratował kolegi :)

Tworzysz plik MyUnit.cpp oraz MyUnit.h
MyUnit.h:

#ifndef MyUnitH
#define MyUnitH
int connection();
#endif

Oraz MyUnit.cpp

#include "MyUnit.h"

#include "definitions.h"
int connection() {
    //WinSock Startup
    WSAData wsaData;
    WORD DllVersion = MAKEWORD(2, 1);
    if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
    {
        MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    SOCKADDR_IN addr; //Address that we will bind our listening socket to
    int addrlen = sizeof(addr); //length of the address (required for accept call)
    addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Broadcast locally
    addr.sin_port = htons(7172); //Port
    addr.sin_family = AF_INET; //IPv4 Socket

    SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections
    bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket
    listen(sListen, SOMAXCONN); //Places sListen socket in a state in which it is listening for an incoming connection. Note:SOMAXCONN = Socket Oustanding Max Connections

    SOCKET newConnection; //Socket to hold the client's connection
    newConnection = accept(sListen, (SOCKADDR*)&addr, &addrlen); //Accept a new connection
    if (newConnection == 0) //If accepting the client connection failed
    {
        std::cout << "Failed to accept the client's connection." << std::endl;
    }
    else //If client connection properly accepted
    {
        std::cout << "Client Connected!" << std::endl;
        char MOTD[256] = "Welcome! This is the Message of the Day."; //Create buffer with message of the day
        send(newConnection, MOTD, sizeof(MOTD), NULL); //Send MOTD buffer
    }

    return 0;

}

Użycie następująco:
Main.cpp

#include "MyUnit.h"

#pragma comment(lib,"ws2_32.lib")


void loading() {
#ifdef _WIN32
    SetConsoleTitle(STATUS_SERVER_NAME);
#endif

    std::cout << STATUS_SERVER_NAME << " - Version " << std::endl;
}

int main(int argc, char *argv[]){

    connection();
    loading();

    system("pause");
    return 0;
}

Tylko musisz pamiętać, że w IDE dodajesz oba pliki Main.cpp oraz MyUnit.cpp do projektu. Oczywiście nie wiem czy się skompiluje ponieważ przez brak definitions.h nie mogę tego sprawdzić, ale raczej powinno być ok.

0

@Mr.YaHooo: podepnę się pod pytanie, ja mam takie jak zrobić zeby aplikacja się nie zamykała?
tzn chodzi o to zeby tylko myszką dało się zamknąć aplikacje po nacisnieciu w rogu na ten x

0
Błękitny Karp napisał(a):

ja mam takie jak zrobić zeby aplikacja się nie zamykała?

Po prostu musisz nie dopuścić aby funkcja main doszła do kończącego returna. Coś w tym stylu można by zrobić:

#include <iostream>

void DoSth(void)
{
  int a,b;

  std::cout << "Podaj a ";
  std::cin >> a;
  std::cout << "Podaj b ";
  std::cin >> b;
  std::cout << "Suma a+b = " << a+b << std::endl;
}

int main(int argc, char* argv[])
{
  for(;;)
  {
    DoSth();
  }

  return 0;
}

W takim przypadku program będzie się kręcił w nieskończoność wykonując funkcję DoSth Tylko wtedy będzie można program zakończyć oprócz kliknięcia w krzyżyk na belce tytułowej za pomocą skrótu Ctrl+C

1 użytkowników online, w tym zalogowanych: 0, gości: 1