Thread: compiling probs, no idea whats up

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

    compiling probs, no idea whats up

    ok i am trying to compile a program, and i am getting a really weird error code which i have no idea what to even look at to solve the problem

    the error code is as follows

    Code:
    Compiling...
    disconnect.cpp
    Linking...
    client.obj : error LNK2001: unresolved external symbol "void __cdecl shutdownClient(int)" (?shutdownClient@@YAXH@Z)
    client.obj : error LNK2001: unresolved external symbol "int __cdecl cconnect(unsigned short,char const *)" (?cconnect@@YAHGPBD@Z)
    Debug/client.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    and this is where i think the problem is from but not sure, this is the main cpp file of the project

    Code:
    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    #include "disconnect.h"
    #include "connect.h"
    
    using namespace std;
    
    int cconnect(unsigned short port, const char* serverName);
    
    void shutdownClient(int clientSocket);
    
    void main()
    {
    	int mysocket;
    
    	mysocket = cconnect(7654, "localhost");			//create client
    
    	if (mysocket == -1) {
    		shutdownClient(mysocket);				//if error occurs shutdown client and close
    		return;
    	}
    
    	int sizebytes;				//is message size and also error code
    
    	#define message_size 256		//how long message will be
    
    	char inMessage[message_size];
    	char outMessage[message_size] = "I sent you this message";			//message buffers
    
    	sizebytes = send(mysocket, outMessage, sizeof(outMessage), 0);			//send message to sever
    
    	if (sizebytes == SOCKET_ERROR) {
    		cout << "Message Send Failed!" << endl;				//check fro errors
    	} else {
    		// print the message we sent
    		printf("Message Sent : %s\n", outMessage);
    	}
    
    	sizebytes = recv(mysocket, inMessage, sizeof(inMessage), 0);		//reply from server
    
    	if (sizebytes == SOCKET_ERROR) {
    		cout << "Recv Failed!" << endl;					//check for errors
    	} else {
    		// print the received message
    		printf("Message Received : %s\n", inMessage);
    	}
    
    	shutdownClient(mysocket);
    
    	cout << "Press any key to continue ..." << endl;
    	getchar();

  2. #2
    Hello,

    Where are the function bodies of those two functions? I see where you prototype them, though I do not see where the functions exist. Are they located in your local header files, or a seperate library?

    Furthermore, a LNK2001 error means that the link editor is looking for a compiled function and can't find it. There are two common reasons for a LNK2001 error:
    • The call in *.cpp doesn't match the function prototype in *.h. The mismatch may be in the function name, return type, or number and/or type of parameters.
    • The function was never declared or was declared but never defined. To see if either is the case go to the ClassView window of the Workspace view. Click the + next to <Class> and find <Function> in the list of member functions.
    Simply, check for the following:
    • Check that the function name is spelled the same (case sensitive) in all your files.
    • Check that the function is actually declared and defined.
    • Check that the number and type of parameters in the function implementation matches the number and type of parameters declared in the function declaration.
    • Check that the number and type of parameters in the function call matches the number and type of parameters declared in the function header.


    - Stack Overflow
    Last edited by Stack Overflow; 01-03-2005 at 11:56 AM. Reason: More info.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    these are the other files in the project where the functions are located

    Code:
    // connect.cpp: implementation of the connect class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "connect.h"
    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    clientconnect::clientconnect()
    {
    
    }
    
    clientconnect::~clientconnect()
    {
    
    }
    
    int clientconnect::cconnect(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;
    
    }
    Code:
    // connect.h: interface for the connect class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_CONNECT_H__28EC6D89_71B9_4440_BA46_68B4136030B6__INCLUDED_)
    #define AFX_CONNECT_H__28EC6D89_71B9_4440_BA46_68B4136030B6__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class clientconnect  
    {
    public:
    	clientconnect();
    	virtual ~clientconnect();
    
    	int cconnect(unsigned short port, const char *serverName);
    
    
    };
    
    #endif // !defined(AFX_CONNECT_H__28EC6D89_71B9_4440_BA46_68B4136030B6__INCLUDED_)
    Code:
    // disconnect.cpp: implementation of the disconnect class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "disconnect.h"
    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    disconnect::disconnect()
    {
    
    }
    
    disconnect::~disconnect()
    {
    
    }
    
    void disconnect::shutdownClient(int clientSocket)
    {
    	
    	closesocket(clientSocket);		// close socket
    
    	WSACleanup();			// shut down winsock
    
    	cout <<"Connection Closed" << endl;
    
    }
    Code:
    // disconnect.h: interface for the disconnect class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_DISCONNECT_H__FC333D22_2437_41E8_99F1_80BCB68C371F__INCLUDED_)
    #define AFX_DISCONNECT_H__FC333D22_2437_41E8_99F1_80BCB68C371F__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class disconnect  
    {
    public:
    	disconnect();
    	virtual ~disconnect();
    
    	void shutdownClient(int clientSocket);
    
    };
    
    #endif // !defined(AFX_DISCONNECT_H__FC333D22_2437_41E8_99F1_80BCB68C371F__INCLUDED_)

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are calling your functions improperly, they are members of a class and not standalone functions. You need to have an instance of the class and then call the member function using the dot operator, i.e:

    Code:
    class A
    {
    public:
        void myfunc();
    };
    
    int main()
    {
        A aclass;  // Declare an instance 'aclass'  of the class
    
        aclass.myfunc();  // Call member function
    }
    Your classes also seem poorly designed. I would make the class handle all of the connect AND disconnect functionality. Making them seperate classes does not seem to make much sense to me.
    Last edited by hk_mp5kpdw; 01-03-2005 at 01:40 PM. Reason: Added missing return type for member function myfunc
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Hello,

    I see these functions are defined inside a class. Functions declared within a class definition are called member functions and can be invoked only for a specific variable of the appropraite type using the standard syntax for structure member access.

    Before using a class, you must declare it first. A class declaration that is not followed by a list of variables reserves no storage; it merely describes a template or the shape of a class. If the declaration is tagged, however, the tag can be used later in defintions of instances of the class.

    For example:
    Code:
    class Date {
    public:
    	int result;
    	void test(void);
    };
    
    int main() {
    	Date myClass; // Declare instance of class
    
    	myClass.test(); // Call class function
    
    	return 0;
    }
    
    void Date::test(void) {
    	result = 5;
    }
    If this makes sense, then it should become clear why your compiler returns two errors. Your functions you are calling are declared within a class. Therefore, you need to create an instance to your class and call on your functions.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Stack: I have to ask you....do you manually colorize your code posts or did you make a parser to do it? Either way, it's really cool that you do that, it would be really nice if we had some sort of built-in php feature here on the boards when people posted code to syntax highlight everything. It wouldn't be too incredbly hard to do.

  7. #7
    Hi,

    I manually color code each object. It roughly takes me 30 minutes to an hour to complete a post depending on the complexity of the count of objects to be colored. I had a friend work on a PHP script to help me parse the code with color, but it contains a few bugs. So, I continue to color my code manually.

    I feel the color helps bring life to the code. Glad you like it!


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Stack you should check this out its pretty good
    http://www.codeguru.com/Cpp/misc/sam...cle.php/c4693/
    Woop?

  9. #9
    Awesome,

    I'll keep this tool in mind. Thanks prog-bman.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer probs. with structs.
    By dinjas in forum C Programming
    Replies: 6
    Last Post: 03-08-2005, 05:59 PM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 03:25 PM
  3. Compiling C++ under C code
    By rjasso in forum C Programming
    Replies: 2
    Last Post: 11-11-2004, 01:44 PM
  4. A little problem about an idea, help please
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 09:52 PM
  5. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 12:09 PM