Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,205 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,350 people online right now. Registration is fast and FREE... Join Now!




Linker Error

 
Reply to this topicStart new topic

Linker Error, 2 Deminsional Arrays and Random Numbers

Cheeto
5 Jul, 2008 - 06:44 PM
Post #1

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 37


My Contributions
I posted this question awhile ago, but didn't really get any answers, and the thread has since been buried. So sorry for the repost crazy.gif

When I attempt to compile, I get " [Linker error] undefined reference to `WarTable:: D6' " and [Linker error] undefined reference to `WarTable:: D3'. I am trying to initialize certain values in my 2 dimensional array to a random number based off of a roll of a D6 or D3.Is there a better way to do this? I originally wrote this program in java and it worked fine but I'm re-writing it in c++ to learn the language better.

I am using Dev-C++ ver4.9


Constructor
CODE

WarTable::WarTable()
{
//for a D6 roll
D6 = rand() % 6 + 1;
//for a D3 roll
D3 = rand() % 3 + 1;
}


Variables are defined here in the header file

CODE


#ifndef _WARTABLE_H_
#define _WARTABLE_H_

class WarTable
{
      public:
             //constructor
             WarTable();
             //D6 roll
             static int D6;
             //D3 roll
             static int D3;
             static const int Soldiers[228][10];
             static const int Weapons[145][4];
             static const int Vehicles[39][8];  
};
#endif


Part of array
CODE

#include "wartable.h"

const int WarTable::Soldiers[228][10] = {
                              //ws,bs,s,t,w,i,a,ld,sv
       /*Abaddon*/               {7,5,8,5,4,6,4,10,2},
       /*Ahriman*/               {5,5,4,4,3,5,3,10,3},                    
       /*Archon*/                {6,6,3,3,3,7,3,9,5},
       /*Ard Boy*/               {4,2,3,4,1,2,2,7,4},
       /*Aspiring Champion*/     {4,4,4,4,1,4,2,10,3},
       /*Aspiring Sorcerer*/     {4,4,4,4,1,4,2,10,3},


Example implementation in the array

CODE

/*Chaos Spawn*/   {3,0,5,5,3,3,D6,10,0},
/*Deffguns*/               {7,4,D3},


Any help would be great smile.gif , this problem has been driving my crazy blink.gif

This post has been edited by Cheeto: 5 Jul, 2008 - 06:46 PM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Linker Error
6 Jul, 2008 - 06:50 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
I remember this from before, and I suggested using the this-> pointer, and you even thanked me... did that not work?
User is offlineProfile CardPM
+Quote Post

Cheeto
RE: Linker Error
6 Jul, 2008 - 12:01 PM
Post #3

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 37


My Contributions
No it didn't work unfortunately, sorry smile.gif . I thanked you because I was so sure it was going to work, doh blink.gif Kinda stupid of me I know....

This post has been edited by Cheeto: 6 Jul, 2008 - 12:03 PM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Linker Error
6 Jul, 2008 - 12:06 PM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
You have a cpp file with your constructor in, are you linking it?

In your cpp file with the constructor, try adding #include "wartable.h"

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

Cheeto
RE: Linker Error
6 Jul, 2008 - 12:21 PM
Post #5

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 37


My Contributions
I already have the header file included in my cpp source file, as you can see on my first post above the array. icon_up.gif

This post has been edited by Cheeto: 6 Jul, 2008 - 12:22 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Linker Error
6 Jul, 2008 - 02:46 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,050



Thanked: 124 times
Dream Kudos: 1200
My Contributions
If I am reading correctly, you are defining those variables in the constructor of the WarTable(). Inside the class declaration, I would make those two variables private and try the following:

cpp

class Wartable {
public:
Wartable(int valOne, int valTwo): D6(valOne), D3(valTwo)
{
//for a D6 roll
valOne = rand() % 6 + 1;
//for a D3 roll
valTwo = rand() % 3 + 1;
}//This is theory and I haven't tested it based on your code
//This is also assuming the "game board" is initialized random on
//startup of each "game"
~WarTable() {} // I also noticed a lack of a defined destructor in your code

private:
int D6;
int D3;
}; //end class


If that doesn't work, there are a few other ways to define values on object creation.

This post has been edited by KYA: 6 Jul, 2008 - 02:46 PM
User is online!Profile CardPM
+Quote Post

Cheeto
RE: Linker Error
6 Jul, 2008 - 03:57 PM
Post #7

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 37


My Contributions
So far this is what I have.

CODE

#ifndef _WARTABLE_H_
#include "special.h"
#define _WARTABLE_H_

class WarTable
{
     public:
          WarTable(int valOne, int valTwo): D6(valOne), D3(valTwo)
          {
             //for a D6 roll
             valOne = rand() % 6 + 1;
             //for a D3 roll
             valTwo = rand() % 3 + 1;
           }
          ~WarTable() {}
          static const int Soldiers[228][10];
          static const int Weapons[145][4];
          static const int Vehicles[39][8];  
    private:
         static int D6;
         static int D3;
}; //end class
#endif


When I compile, I get "int WarTable:: D6' is a static data member; it can only be initialized at its definition" and the same thing for D3. But when I make D6 a non-static variable (along with the arrays), I get errors for the arrays not being static. crazy.gif . Maybe I'm a bit confused what making a variable static means, doesn't it just give a variable broader scope?

I have my arrays in the cpp file instantiated by as such

CODE

const int WarTable::Soldiers[228][10]
//values listed after
const int WarTable::Weapons[145][4]
//values listed after
const int WarTable::Vehicles[39][8]
//values listed after

Thanks for the help smile.gif .

User is offlineProfile CardPM
+Quote Post

KYA
RE: Linker Error
6 Jul, 2008 - 04:13 PM
Post #8

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,050



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Have you tried making your arrays not const?

EDIT:

Try this:

cpp

class WarTable
{
public:
WarTable(): D6(rand() % 6 + 1), D3(rand() % 3 +1) {}
~WarTable() {}
int Soldiers[228][10];
int Weapons[145][4];
int Vehicles[39][8];
private:
int D6;
int D3;
}; //end class
#endif


This post has been edited by KYA: 6 Jul, 2008 - 04:18 PM
User is online!Profile CardPM
+Quote Post

Cheeto
RE: Linker Error
6 Jul, 2008 - 06:42 PM
Post #9

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 37


My Contributions
Now I have my arrays instantiated in the cpp file as.

CODE

WarTable::Soldiers[228][10] = {
//more values not show here
WarTable::Weapons[145][4] ={
//more values not show here
WarTable::Vehicles[39][8] ={
//more values not show here


But when I compile it says "expected `,' or `;' before '=' token" and "expected constructor, destructor, or type conversion before '=' token". I checked all of the semi-colons after the arrays and they are all there. Do I have to add them to the constructor? The purpose of this class is so that the client can reference these three arrays. The reason I made the arrays constant is because the values in the arrays should never be modified after creation, only referenced.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Linker Error
7 Jul, 2008 - 06:31 AM
Post #10

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,050



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Those arrays are not functions and do not need the scope operator "::", try ":" instead.
User is online!Profile CardPM
+Quote Post

Cheeto
RE: Linker Error
8 Jul, 2008 - 03:25 PM
Post #11

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 37


My Contributions
Now I get a "expected unqualified-id before ':' token" error.


Declared
CODE

int Soldiers[228][10];


in cpp file
CODE

WarTable:Soldiers[228][10] = {


I have no idea what this error means and so far google has proved unhelpful.

Thanks smile.gif

This post has been edited by Cheeto: 8 Jul, 2008 - 03:26 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month