Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct arrays in headers--- Please help

Status
Not open for further replies.

ToddT

Programmer
Feb 7, 2003
14
US
I made an array of struct and put it in a header file in visual c++. it gave me 300 errors. I couldn't find anything wrong with code, so I pasted into a stump main file. then I had no errors. does anyone know how I cain remove the table from the main program without recieveing errors?
 
We need a bit more information.

1. Did you include the header file in the area you wanted to use it?

2. Did you remember the semicolon at the end of the struct declaration.

3. Did the area you declared the array know of the struct

Basically, what were the errors?

Matt
 
I am getting a zero sized array constant. The rest of the errors are just do to the array being unrecognized. The header file is included. Here's some code:

struct op_table{
string Name;
string OpCode;
string Rs;
string Rt;
string Rd;
string Shamt;
string Funct;
};

op_table OPTAB[31];

OPTAB[0].Name = "add";
OPTAB[0].OpCode = "000000";
OPTAB[0].Funct = "100000";
OPTAB[0].Shamt = "00000";

OPTAB[1].Name = "sub";
OPTAB[1].OpCode = "000000";
OPTAB[1].Shamt = "00000";
OPTAB[1].Funct = "100010";

OPTAB[2].Name = "addu";
OPTAB[2].OpCode = "000000";
OPTAB[2].Shamt = "00000";
OPTAB[2].Funct = "100001";

OPTAB[3].Name = "subu";
OPTAB[3].OpCode = "000000";
OPTAB[3].Shamt = "00000";
OPTAB[3].Funct ="100011";
.
.
.
it goes on but the first error is on
op_table OPTAB[31];
 
You cannot access the OPTAB[31] object! Only ones with 0...30.
To answer to your question here is a working solution with less impact on your code:
Create an "hello world" console application named for example tek05 e.g. the following files will be generated:
Code:
stdafx.h
--------
#if !defined(AFX_STDAFX_H__)
#define AFX_STDAFX_H__)

#include <stdio.h>

#endif

stdafx.cpp
---------- 

#include &quot;stdafx.h&quot;

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file


tek05.cpp
---------
// tek05.cpp : Defines the entry point for the console application.
//
#include &quot;stdafx.h&quot;
int main(int argc, char* argv[])
{
  printf(&quot;Hello World!\n&quot;);
  return 0;
}

Now , add new header file named : optable.h 

optable.h
---------
#if !defined(__OPTABLE__)
#define __OPTABLE__

struct op_table{
    string Name;
    string OpCode;
    string Rs;
    string Rt;
    string Rd;
    string Shamt;
    string Funct;
	op_table(){};
	static void FillOpTable(op_table arr[], int dim)
	{
		int i=0;
		// add code to control the index
		arr[0].Name = &quot;add&quot;;
		arr[0].OpCode = &quot;000000&quot;; 
		arr[0].Funct = &quot;100000&quot;;
		arr[0].Shamt = &quot;00000&quot;;

		arr[1].Name = &quot;sub&quot;;
		arr[1].OpCode = &quot;000000&quot;;
		arr[1].Shamt = &quot;00000&quot;;
		arr[1].Funct = &quot;100010&quot;;
		///
	};
};

#endif

Modify stdafx.h as follows:
stdafx.h
--------
#if !defined(AFX_STDAFX_H__)
#define AFX_STDAFX_H__)

#include <stdio.h>
#include <iostream>
using std::string;
#include &quot;optable.h&quot;
#ndif

Modify the tek05.cpp as follows:
tek05.cpp
---------
// tek05.cpp : Defines the entry point for the console application.
//

#include &quot;stdafx.h&quot;
int main(int argc, char* argv[])
{
	op_table OPTAB[31];
	op_table::FillOpTable(OPTAB, sizeof(OPTAB)/sizeof(op_table));
 	return 0;
}

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top