Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is mine A.h file
I want to make it as an Base class
C++
<pre lang="c++">class A
{
public     :
    struct addrinfo    addr_;
    unsigned short          port_;
    CString                  hostname_;
    CRITICAL_SECTION        sect_;
    HANDLE                  threadHandle_;
        HANDLE                  threadHandle1_;
    bool                    connected_;
    SOCKET          sock_;
public:
     int ConnectToMachine();  //This will make socket connection with the machine.
};


This is mine A.cpp
C++
class B:public A
{
int ConnectToMachine()
	{
		int conResult,iResult;
		struct addrinfo           *result = NULL,*ptr = NULL;
	  	
		u_long iMode = 0;
		DWORD nTimeout = 5000; // 5 seconds
		int port=22;
		WSADATA wsaData;

		// Initialize Winsock
		
		iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
		if (iResult != 0) 
		{
			printf("WSAStartup failed with error: %d\n", iResult);

		}

		ZeroMemory( &addr_, sizeof(addr_) );
		addr_.ai_family = AF_UNSPEC;
		addr_.ai_socktype = SOCK_STREAM;
		addr_.ai_protocol = IPPROTO_TCP;

		// Resolve the server address and port
		conResult = getaddrinfo("192.168.15.168", (PCSTR)22, &addr_, &result);
		if ( conResult != 0 ) {
			printf("getaddrinfo failed with error: %d\n", conResult);
			WSACleanup();
			return 1;
		}

		// Attempt to connect to an address until one succeeds
		for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

			// Create a SOCKET for connecting to server
			sock_ = socket(ptr->ai_family, ptr->ai_socktype, 
				ptr->ai_protocol);
			if (sock_ == INVALID_SOCKET) {
				printf("socket failed with error: %ld\n", WSAGetLastError());
				WSACleanup();
				return 1;
			}


			conResult = ioctlsocket(sock_, FIONBIO, &iMode);
			if (conResult != NO_ERROR)
				printf("ioctlsocket failed with error: %ld\n", conResult);
			

			conResult = setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
			if (conResult != NO_ERROR)
			{
				printf("\nSetsocopt fail with error :%d\n",WSAGetLastError());

				return 0;
			}


			// Connect to server.
			conResult = connect(sock_, ptr->ai_addr, (int)ptr->ai_addrlen);
			if (conResult == SOCKET_ERROR) {
				closesocket(sock_);
				sock_ = INVALID_SOCKET;
				continue;
			}
			break;
		}

		freeaddrinfo(result);

		if (sock_ == INVALID_SOCKET) {
			printf("Unable to connect to server!\n");
			WSACleanup();
			return 1;
		}
		//DWORD dwThreadId;
		//threadHandle_=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ServerConnectThread,(LPVOID)sock_,0,&dwThreadId);
			//threadHandle_=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,this,0,&dwThreadId);
		DWORD dwThreadId;
	threadHandle1_=CreateThread(NULL,0,startReceive,this,0,&dwThreadId);

	}
}


int main()
{
//i want to call the connect to machine function of B class,how to do this,simple creating the object of B class doesn't work

}
Posted
Comments
Sergey Alexandrovich Kryukov 11-Oct-12 12:19pm    
Do I have to report it as off-topic? Can you see why I am asking?

Because this forum is for software developers and students. And a software developer don't say "does not work", but provides a comprehensive issue report. What's not working, exactly? Compiler errors? warnings? exception during run time? what? Yes, we can probably figure it out, but most would not bother if you do not collaborate.
--SA
Tarun Batra 11-Oct-12 12:23pm    
Sorry for the wrong question i want to declare two functions in class A and implement them in class B and call the function of class Bi did like this class B obj;int main(){
obj.connecttomachine();
}but it says obj has no members available
Sergey Alexandrovich Kryukov 11-Oct-12 12:38pm    
"It says"... (sigh...) Can you ever report things exactly? You can 1) comment a line in your code and 2) copy and paste exact error message or comprehensive exception information. For this purpose, use "Improve question".
--SA
aasikRaja 12-Oct-12 0:40am    
Hello Tarun..I checked your code and assure that it will work..u can declare two functions in Class A and define those in class B. And you can create object for Class B in main() function. This object will allow you to call those functions you defined in class B. check any compilation error. If still same problems, post with error details..

1 solution

In C++, member functions are private by default and so you cannot call ConnectToMachine() with class B's object. You'll end up with two solutions:-


C++
class B:public A
{
  public:
   int ConnectToMachine(){...}
}


OR

C++
struct B:public A
{

   int ConnectToMachine(){...}
}
 
Share this answer
 
Comments
Tarun Batra 11-Oct-12 13:11pm    
i did some thing like this now :
this is a.h
#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#include<atlstr.h>
#include<windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;
class IDialysisConnector
{
public :
struct addrinfo addr_;
unsigned short port_;
CString hostname_;
CRITICAL_SECTION sect_;
HANDLE threadHandle_;
HANDLE threadHandle1_;
bool connected_;

public :
SOCKET sock_;
IDialysisConnector()
{
//WSADATA wsaData;

//// Initialize Winsock
//int iResult;
//iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
//if (iResult != 0)
//{
// printf("WSAStartup failed with error: %d\n", iResult);

//}


}


public:
virtual int ConnectToMachine()=0;
};
class B:public A
{
public:
int ConnectToMachine()
{
int conResult,iResult;
struct addrinfo *result = NULL,*ptr = NULL;

u_long iMode = 0;
DWORD nTimeout = 5000; // 5 seconds
int port=22;
WSADATA wsaData;

// Initialize Winsock

iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);

}

ZeroMemory( &addr_, sizeof(addr_) );
addr_.ai_family = AF_UNSPEC;
addr_.ai_socktype = SOCK_STREAM;
addr_.ai_protocol = IPPROTO_TCP;

// Resolve the server address and port
conResult = getaddrinfo("192.168.15.168", (PCSTR)22, &addr_, &result);
if ( conResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", conResult);
WSACleanup();
return 1;
}

// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

// Create a SOCKET for connecting to server
sock_ = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (sock_ == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
/////////////////////////////////////////////////////////////////////////////
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled;
// If iMode != 0, non-blocking mode is enabled.

conResult = ioctlsocket(sock_, FIONBIO, &iMode);
if (conResult != NO_ERROR)
printf("ioctlsocket failed with error: %ld\n", conResult);
/////////////////////////////////////////////////////////////////////////////


conResult = setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
if (conResult != NO_ERROR)
{
printf("\nSetsocopt fail with error :%d\n",WSAGetLastError());

return 0;
}


// Connect to server.
conResult = connect(sock_, ptr->ai_addr, (int)ptr->ai_addrlen);
if (conResult == SOCKET_ERROR) {
closesocket(sock_);
sock_ = INVALID_SOCKET;
continue;
}
break;
}

freeaddrinfo(result);

if (sock_ == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
}
};
#include"a.h"
int main()
{
B *pChecking = new B;
pChecking->ConnectToMachine();
}

when i run it i get these errors:
Main.obj : error LNK2019: unresolved external symbol __imp__freeaddrinfo@4 referenced in function "public: virtual int __thiscall CDiamaxConnector::ConnectToMachine(void)" (?ConnectToMachine@CDiamaxConnector@@UAEHXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: virtual int __thiscall CDiamaxConnector::ConnectToMachine(void)" (?ConnectToMachine@CDiamaxConnector@@UAEHXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function "public: virtual int __thiscall CDiamaxConnector::ConnectToMachine(void)" (?ConnectToMachine@CDiamaxConnector@@UAEHXZ)
1>Main.obj : error LNK2019: unresolved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900