Linker Error

2 Deminsional Arrays and Random Numbers

Page 1 of 1

10 Replies - 1717 Views - Last Post: 08 July 2008 - 04:25 PM Rate Topic: -----

#1 Cheeto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-March 08

Linker Error

Posted 05 July 2008 - 07:44 PM

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:

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
 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

#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
#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

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



Any help would be great :) , this problem has been driving my crazy :blink:

This post has been edited by Cheeto: 05 July 2008 - 07:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Linker Error

#2 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Linker Error

Posted 06 July 2008 - 07:50 AM

I remember this from before, and I suggested using the this-> pointer, and you even thanked me... did that not work?
Was This Post Helpful? 0
  • +
  • -

#3 Cheeto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-March 08

Re: Linker Error

Posted 06 July 2008 - 01:01 PM

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

This post has been edited by Cheeto: 06 July 2008 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Linker Error

Posted 06 July 2008 - 01:06 PM

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 :)
Was This Post Helpful? 0
  • +
  • -

#5 Cheeto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-March 08

Re: Linker Error

Posted 06 July 2008 - 01:21 PM

I already have the header file included in my cpp source file, as you can see on my first post above the array. :^:

This post has been edited by Cheeto: 06 July 2008 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Linker Error

Posted 06 July 2008 - 03:46 PM

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:

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: 06 July 2008 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 Cheeto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-March 08

Re: Linker Error

Posted 06 July 2008 - 04:57 PM

So far this is what I have.

#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: . 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

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 :) .
Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Linker Error

Posted 06 July 2008 - 05:13 PM

Have you tried making your arrays not const?

EDIT:

Try this:

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: 06 July 2008 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 Cheeto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-March 08

Re: Linker Error

Posted 06 July 2008 - 07:42 PM

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

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.
Was This Post Helpful? 0
  • +
  • -

#10 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Linker Error

Posted 07 July 2008 - 07:31 AM

Those arrays are not functions and do not need the scope operator "::", try ":" instead.
Was This Post Helpful? 0
  • +
  • -

#11 Cheeto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-March 08

Re: Linker Error

Posted 08 July 2008 - 04:25 PM

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


Declared
int Soldiers[228][10];



in cpp file
WarTable:Soldiers[228][10] = {



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

Thanks :)

This post has been edited by Cheeto: 08 July 2008 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1