simple problem, please help

This is a discussion on simple problem, please help within the C++ Programming forums, part of the General Programming Boards category; ok i have not coded in c++ for nearly a year now, and i have forgotten quite a bit and ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    simple problem, please help

    ok i have not coded in c++ for nearly a year now, and i have forgotten quite a bit and i can't remember how to solve a problem

    i get the following error code

    C:\Documents and Settings\chris285\Desktop\client\connect.cpp(64) : error C2661: 'connect::connect' : no overloaded function takes 3 parameters

    on this piece of code

    Code:
    // connect.cpp: implementation of the connect class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "connect.h"
    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    connect::connect()
    {
    
    }
    
    connect::~connect()
    {
    
    }
    
    int connect::startclient(unsigned short port, const char *serverName)
    {
    	int error;
    
    	WSAData wsaData;	// the winsock data structure
    
    	
    	if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
    		cout << "Winsock Start Up Failed!" << endl;							// startup winsock
    		return -1;
    	}
    
    	
    	int mySocket = socket(AF_INET, SOCK_STREAM, 0);		// create my socket
    
    	
    	if (mySocket == SOCKET_ERROR) {
    		cout << "Error Opening Socket!" << endl;			// make sure nothing bad happened
    		return -1;
    	}
    
    	struct hostent *host_entry;
    
    
    	if ((host_entry = gethostbyname(serverName)) == NULL) {				// setup the host entry
    		cout << "Could not find host!" << endl;
    	}
    
    	struct sockaddr_in server;
    
    	
    	server.sin_family = AF_INET;
    	server.sin_port = htons(port);									// fill in the server socket info
    	server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
    
    	
    	if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {		// connect to the server
    		cout<< "Error connecting to server!" << endl;
    	}
    
    	cout << "Connection Established" << endl;
    
    	return mySocket;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    That is because you want to call the function connect, but instead you call the constructor to your connect class. Change the classname (and constructors) to something like CConnect

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    thanks for the help, problem sorted

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    84

    o-o'd code, same prob and can't see whats up

    ok i have o-o'd part of my code now, but i am still getting the error function cannot take 3 parameters and i can't see whats worng despite fixing it before

    Code:
    // connecting.cpp: implementation of the connecting class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "connecting.h"
    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    connecting::connecting()
    {
    
    }
    
    connecting::~connecting()
    {
    
    }
    
    
    int connecting::connect(unsigned short port, const char *servername)
    {
    	int error;
    
    	WSAData wsaData;	// the winsock data structure
    
    	
    	if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
    		cout << "Winsock Start Up Failed!" << endl;							// startup winsock
    		return -1;
    	}
    
    	
    	int mySocket = socket(AF_INET, SOCK_STREAM, 0);		// create my socket
    
    	
    	if (mySocket == SOCKET_ERROR) {
    		cout << "Error Opening Socket!" << endl;			// make sure nothing bad happened
    		return -1;
    	}
    
    	struct hostent *host_entry;
    
    
    	if ((host_entry = gethostbyname(servername)) == NULL) {				// setup the host entry
    		cout << "Could not find host!" << endl;
    	}
    
    	struct sockaddr_in server;
    
    	
    	server.sin_family = AF_INET;
    	server.sin_port = htons(port);									// fill in the server socket info
    	server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
    
    	
    	if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {		// connect to the server
    		cout<< "Error connecting to server!" << endl;
    	}
    
    	cout << "Connection Established" << endl;
    
    	return mySocket;
    }
    
    
    void connecting::disconnect(int clientSocket)
    {
    	closesocket(clientSocket);		// close socket
    
    	WSACleanup();			// shut down winsock
    
    	cout <<"Connection Closed" << endl;
    
    }
    the header file

    Code:
    // connecting.h: interface for the connecting class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_CONNECTING_H__866BEDBC_0F7C_4EA2_8259_3D5C35DEF3DB__INCLUDED_)
    #define AFX_CONNECTING_H__866BEDBC_0F7C_4EA2_8259_3D5C35DEF3DB__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class connecting  
    {
    public:
    	connecting();
    	virtual ~connecting();
    
    	int connect(unsigned short port, const char *serverName);
    
    	void disconnect(int clientSocket);
    
    };
    
    #endif // !defined(AFX_CONNECTING_H__866BEDBC_0F7C_4EA2_8259_3D5C35DEF3DB__INCLUDED_)

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    I have just skimmed through your code very fast but show me how you call connecting::connect in your program, that is my guess where the problem is.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    84
    i haven't called it yet in the program, i just want to make sure there are no sytax probs etc

    i want it to be like a connection class, with a function in the class for connect and disconnecting from the server

    what names would u suggest calling them?

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    I believe your problem is right here:
    Code:
    if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {		// connect to the server
    		cout<< "Error connecting to server!" << endl;
    	}
    My guess would be that when you call connect here, the compiler thinks you are trying to call connect in your class but you want to call the winsock connect. Just change the connectfunction in your class to something like Connect, or ConnectToServer or something like that.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    84
    thanks for that, now i get a problem in the main when i call the function from the class. it says function does not take 0 perameters

    Code:
    #include "connecting.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	connecting client;
    
    	client.connecttoserver();
    
    	client.disconnectfromserver();
    
    	return 0;
    
    }

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    You need to send what port and the servername to the function. The function takes 2 parameters:
    Code:
    int connecting::connect(unsigned short port, const char *servername)

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    84
    ok i am unsure how to do that, i thought i had solved it but obviously not

    now added the following files to the code

    Code:
    // mesagesend.cpp: implementation of the mesagesend class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "mesagesend.h"
    #include "connecting.h"
    #include <iostream>
    #include <winsock2.h>
    #include <stdio.h>
    
    using namespace std;
    
    	int connecttoserver(unsigned short port, const char *serverName);
    
    	void disconnectfromserver(int clientSocket);
    
    
    
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    mesagesend::mesagesend()
    {
    
    }
    
    mesagesend::~mesagesend()
    {
    
    }
    
    void mesagesend::sendmessage()
    {
    	mysocket = connecttoserver(7654, "localhost");
    
    	if (mysocket == -1) {
    		disconnectfromserver(mysocket);			//if error occurs shut down client
    		return;
    	}
    
    	#define message_size 256
    
    	char inmessage[message_size];
    	char outmessage[message_size] = "I sent you this message";
    
    	nbytes = send(mysocket, outmessage, sizeof(outmessage), 0);
    
    	if (nbytes == SOCKET_ERROR) {
    		cout << "Message Send Failed!" << endl;
    	} else {
    		// print the message we sent
    		printf("Message Sent : %s\n", outmessage);
    	}
    
    	nbytes = recv(mysocket, inmessage, sizeof(inmessage), 0);
    
    	if (nbytes == SOCKET_ERROR) {
    		cout << "Recv Failed!" << endl;
    	} else {
    		// print the received message
    		printf("Message Received : %s\n", inmessage);
    	}
    
    	disconnectfromserver(mysocket);	
    
    	printf("Press any key to continue ...\n");
    	getchar();
    
    
    
    }
    Code:
    // mesagesend.h: interface for the mesagesend class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_MESAGESEND_H__FFA5479E_0367_4BB4_A1EA_B313692D5510__INCLUDED_)
    #define AFX_MESAGESEND_H__FFA5479E_0367_4BB4_A1EA_B313692D5510__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class mesagesend  
    {
    public:
    	mesagesend();
    	virtual ~mesagesend();
    
    	void sendmessage();
    
    	int mysocket;
    
    	int nbytes;
    
    };
    
    #endif // !defined(AFX_MESAGESEND_H__FFA5479E_0367_4BB4_A1EA_B313692D5510__INCLUDED_)
    any help is greatfully accepted

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Could you be more specific what is wrong, include errors and such.

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    84
    ok i build the client and i get the following errors

    C:\Documents and Settings\chris285\Desktop\tutorials\Sending and Receiving\Client\main.cpp(17) : error C2660: 'connecttoserver' : function does not take 0 parameters
    C:\Documents and Settings\chris285\Desktop\tutorials\Sending and Receiving\Client\main.cpp(21) : error C2660: 'disconnectfromserver' : function does not take 0 parameters
    Error executing cl.exe.

    on these lines of code in the main.cpp file

    Code:
    #include "connecting.h"
    #include <iostream>
    #include "mesagesend.h"
    
    #include <winsock2.h>
    
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    	connecting client;
    
    	mesagesend chat;
    
    	client.connecttoserver();
    
    	chat.sendmessage();
    
    	client.disconnectfromserver();
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    see my answer a few posts above

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    84
    how would this be done?

    would it be stated in the function? e.g. port == kinda thing?

    trouble is the ip address of the pc is going to be differnt on each pc as it will be used on my uni network, so really i need a way for the program to find the computers ip address and what not

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21