linker error

This is a discussion on linker error within the C Programming forums, part of the General Programming Boards category; Can anyone tell me what's wrong with this code? Code: *class.h* #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <string.h> class ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    15

    linker error

    Can anyone tell me what's wrong with this code?
    Code:
    *class.h*
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <string.h>
    
    class Decryption{
    public:
    	Decryption();
    	~Decryption();
    	char* decrypt_line(char* buffer);
    };
    ##################################
    *class.cpp*
    
    #include "class.h"
    
    char* Decryption::decrypt_line(char* buffer){
    	//TO DO: not sure about encription type
    	BYTE xor_key;
    	for(int i=0;i<sizeof(buffer);i++){
    		buffer[i] = buffer[i] ^ xor_key;
    	}
    	return(buffer);
    };
    
    *main.cpp*
    
    #include "class.h"
    
    void main(){
    	Decryption *object = new Decryption;
    }
    It compiles fine but throw linker errors:
    Code:
    1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Decryption::Decryption(void)" (??0Decryption@@QAE@XZ) referenced in function _main
    ...\Debug\main.exe : fatal error LNK1120: 1 unresolved externals
    1>main - 2 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I just started writing OO code so I'm not very good at it yet.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > void main()
    main returns an int.

    Also, you need to add class.cpp to the project, so it gets compiled along with main.cpp.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Salem
    Also, you need to add class.cpp to the project, so it gets compiled along with main.cpp.
    Either that or you forgot to implement the constructor and destructor of Decryption.
    Kurt

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    i included all the files and still nothing.....about the constructor..... how do I do that?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    your class doesn't need any constructor or destructor so
    Code:
    class Decryption{
    public:
    	Decryption(){}
    	~Decryption(){}
    	char* decrypt_line(char* buffer);
    };
    should be ok, or just delete the declaration. It's just that if you declare them you have to implement them as well.
    Kurt

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    these are the functions you prototyped in the header:
    Decryption();
    ~Decryption();
    char* decrypt_line(char* buffer);

    these are the ones you actually implemented in the cpp file:
    char* Decryption::decrypt_line(char* buffer)

    if you dont know how to implement a constructor give it a guess, or search cplusplus.com for the tutorial or another resource. good luck!

    also, do you ever dynamically allocate memory in the decryption class? ie do you ever call malloc in the complete code (or do you plan on it)? if you dont call it, you dont need a descructor, if you do, then keep your destructor.

    hope it helps

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    That helped....thanks everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 12:54 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

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