473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Unresolved external symbol error :-(

Hi,

I was just wondering if anyone could explain in simple terms what these errors are, and why they come about.

More specifically if you could explain why this code:
Expand|Select|Wrap|Line Numbers
  1. static const int SIZE = 8*sizeof(unsigned);
  2.     static int* mask[SIZE];
  3. public: static void InitMask()
  4.     {
  5.         int num = 1;
  6.         for (int i=0;i<SIZE;i++)
  7.         {
  8.             mask[i] = &num;
  9.             num=num*2;
  10.         }
  11.     }
produces this error
Expand|Select|Wrap|Line Numbers
  1. main.obj : error LNK2001: unresolved external symbol "private: static int * * Binary::mask" (?mask@Binary@@0PAPAHA)
  2. fatal error LNK1120: 1 unresolved externals
whenever I try to call it from my main.cpp using Binary::InitMask(); (Binary is the class name where this function is declared. I've also tried calling this function using a Binary object eg binObj.InitMask(); The error is the same.

I've only had a few weeks of C++ so pointers etc are a little confusing after learning Java, so the simpler the explanation the better!!
Oct 28 '08 #1
10 7703
AmeL
15
Expand|Select|Wrap|Line Numbers
  1. main.obj : error LNK2001: unresolved external symbol "private: static int * * Binary::mask" (?mask@Binary@@0PAPAHA)
  2. fatal error LNK1120: 1 unresolved externals
Well, the error happens when your linker tries to link your object file ( let says main.o) with the library and with something you have implemented. When the linker read Binary::InitMask(), it can not find where the implementation of InitMask() is. So the linking error takes place.
Well, let says your design looks like this :
  • Binary.h : this is where you have declare the interface of class Binary.
  • Binary.cpp : this is where you implement the class members.
  • main.cpp : this is where you instantiate the object from class Binary.
If your design looks like this, make sure that in your main.cpp, u did include the header file (Binary.h), else your linker can't find the implementation.

Thanks
/AmeL
Oct 28 '08 #2
JosAH
11,448 Expert 8TB
When you define your class in your .h file you only declare its members. You
have to define them in your .cpp file; so you have to define your static int array
in your .cpp file and you're in business.

kind regards,

Jos

ps. only inline members are defined inside the clas definition.
Oct 28 '08 #3
Thanks for your input. I'm still new to C++ andthe whole .h .cpp files and when to use which.

So in the .h file I should declare all the possible variable that my class will use, private and public.

In the .cpp of the class I should implement all of my functions?

Can the .cpp file access the private data members declared in the .h then? Because that is the main confusion I have. When I create an accessor method I put it in the .h because I always thought the .cpp was unable to access the private members.

With the code above it is all in the Binary.h file and the calling is in the main.cpp. The main.cpp cannot be touched by me, it is a standard test, so I can't declare things within it or anything. I am just experimenting to try and see if the mask function I implemented is actually working, and so far it isn't :(
Oct 28 '08 #4
Banfa
9,065 Expert Mod 8TB
It is normal when defining a class to use 2 files, 1 header (.h) and 1 source (.cpp) file.

The header contains the class declaration, that is the

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3. public
  4.     MyClass();
  5.     ~MyClass();
  6.     Print();
  7.  
  8. private:
  9.     int value;
  10. }
  11.  
the source file contains the function (or method) definitions for the class as well as static data declarations if the class has any static data members

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. MyClass::MyClass() : value(42)
  4. {
  5. }
  6.  
  7. MyClass::~MyClass()
  8. {
  9. }
  10.  
  11. MyClass::Print()
  12. {
  13.     std::cout << value;
  14. }
  15.  
the implementation of the function can be placed in the header file inside the class declaration but this makes them inline functions. As such you need to be careful and generally I only put functions that have 1 or 2 lines of source into the class declaration in the header file (accessor methods often fit this category).

Access to the private data and function members of the class is nothing to do with what file you are in but is to do with the scope you are in. You have to be in the scope of the class to access private members, for instance in the MyClass::Print function definition I can access value because the code for that function is in the scope of the class because the function is a member of the class. This function

Expand|Select|Wrap|Line Numbers
  1. int GetValueAdd1(MyClass &mc)
  2. {
  3.     return mc.value+1;
  4. }
  5.  
will always cause a compile error no matter where it is placed (class header, class source or any other file) because it is try to access a private member of MyClass but it is not a member (or friend) of MyClass so it's definition is not in scope for the class.
Oct 28 '08 #5
Banfa
9,065 Expert Mod 8TB
BTW in your original code the way you are declaring and initialising mask is wrong and you will not get the results you expect (assuming that the program doesn't just crash).
Oct 28 '08 #6
Thanks very much again for your insight. It now makes more sense. We had none of this explained to us, which is evident based on the amount of people on this unit having real trouble with this task.

Without going into coding detail or how to do it correctly, could you explain why my mask is wrong?

I thought I could declare an array by simply saying

int myArray[5]; like I could in Java.

Then in a simple loop I could add a number. So in this case I thought that the code

Expand|Select|Wrap|Line Numbers
  1. int myArray[5];
  2. for (int i=0;i<5;i++)
  3. {
  4.   myArray[i]=i;
  5. }
would have the same effect of int myArray[] = {0,1,2,3,4];

in that both of them would produce an array of size 5 which I could then access.

On a side note. If I declared my array in m header, then in my .cpp I created a Binary::InitMask() function. Am I correct in assuming I could then use it within other functions by simply calling InitMask() , or does it still require a calling object?
Oct 28 '08 #7
Banfa
9,065 Expert Mod 8TB
I thought I could declare an array by simply saying

int myArray[5]; like I could in Java.
Yes you could declare it like that but you didn't, look at your own code in the first post.

On a side note. If I declared my array in m header, then in my .cpp I created a Binary::InitMask() function. Am I correct in assuming I could then use it within other functions by simply calling InitMask() , or does it still require a calling object?
By other functions do you mean other class member functions or just other functions in the program?

In other member functions of the class you can access the array directly because it is in scope for them. From the definition of (code for) another class member function you can call InitMask directly without requiring a new instance of the class as you are already working with an instance of the class.

If InitMask is a public function of the class you can call it from any function in the program that has access to a instance of the class.

Sorry your question is vague it makes it hard to answer.
Oct 28 '08 #8
Yes sorry about that. The whole pointer thing was me trying to figure out my error to no avail. I just forgot to take it back out again before pasting the code here.

Regarding the calling of InitMask() within another function.

In my Binary.h If I simply have:

Expand|Select|Wrap|Line Numbers
  1. static void InitMask();
  2. static unsigned getMaskValue(int v);
Then in Binary.cpp I would declare my mask object?

private static unsigned mask[5];

Expand|Select|Wrap|Line Numbers
  1. my InitMask() function, for this example
  2. {
  3.     for (int i=0;i < 5;i++)
  4.          {
  5.           mask[i] = i;
  6.          }
  7. }
  8.  
  9. static unsigned getMaskValue(int v)
  10. {
  11.     return mask[v];
  12. }
Could I then, in my main method simply run:

Expand|Select|Wrap|Line Numbers
  1. Binary::InitMask();
  2. unsigned test = Binary::getMaskValue(2);
  3. cout << test << endl;
in order to access and output a value in my array mask? Or do I still need a declared calling object to call InitMask?
Oct 28 '08 #9
Banfa
9,065 Expert Mod 8TB
Are you creating a class to hold the mask variable or are you just using global functions?

In the first case you are missing all the class nomenclature from your code (the MyClass:: etc), in both cases the keyword private on your array declaration is not required (private is a keyword that I believe is only valid when used in/with a class declaration) it should be used when declaring the array in the class declaration.

From your main code it appears you are putting these things into the Binary class, take a look at the example MyClass I posted, which is (I hope) compilable, for syntax.
Oct 28 '08 #10
I'm beginning to get the hang of it now thank you very much for all your patience and understanding.

I finally figured out what the mask is used for. We are finding the inverse of a number. For that we need to be able to set individual bits on and off using an On(int i) function and an Off(int i) function respectively.

To do this it seems a little clearer now. Say I wanted to set bit(3) to 1.

I can just add my current Binary object/unsigned to the mask[3].

eg I have Binary object binNumber of int value 1, binary 0......001;

if I did binNumber += mask[3]; // mask[3] being int 8, binary 100;

am I correct in assuming it would add 8 to 1, 100 to 001 giving 9 or 101?

I'm getting there, finally understanding the correct calling procedure, I think it was not referring to the class that was holding me up quite a lot!
Oct 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rodolphe | last post by:
Hello, I'm French so sorry for my approximate English. When I try to compile a project under Visual C++ 6.0, I've got the following errors : applicap.obj : error LNK2001: unresolved external...
0
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString...
5
by: cschettle | last post by:
I think you need to link with msvcrt.lib ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
by: Maydogg6 | last post by:
I need a hand with some stubborn link errors. I'm trying to recreate and old program from 6.0 into .NET, but for some reason when I try to compile I'm getting linking errors for all my function...
6
by: sadegh | last post by:
Hi I have a problem with my program in VC++6 When I compile it, the following errors are listed. I spend a lot of time on the groups.google.com to find its reason, but none of comments could...
5
by: bonnielym84 | last post by:
Im new here..didnt noe whether is this the rite way to post my problem..Really need help here..i've been stucked in this error from last wk..My problem is like this..Im using VC++ 6.0 to compile my C...
0
by: bonnielym84 | last post by:
Im new here and im not sure whether is this the right place for me to post my question..anyway..hope that you can help me..i have been stucked in this problem since last wk..My problem is..I'm using...
0
by: Ryan Gaffuri | last post by:
hlink72@hotmail.com (Eric) wrote in message news:<ab8d8b14.0308220550.54fb5f22@posting.google.com>... LNK1120 is a standard C++ error. you using Visual C++? Means your references a class that...
2
by: =?Utf-8?B?YmFzaA==?= | last post by:
Hello, I am compiling a CPP code using Visual studion .net 2003. I get the following error, despite having windldap.h and wldap32.dll in my include and lib paths. Here is the error. uuid.lib...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.