-1

I am using C++Builder XE4. I am trying to compile some C code into a console application. The C file is large so I have tried to focus on the problem. The code sets up two structures and then tries to call a value which is failing.

struct ephloc
{
  long first_item_ordinal;
  long last_item_ordinal;
  int days_per_record;
  int items_per_record;
  int total_records;
};

struct ephloc objs[15] = {
  {    641, 2210500,  8, 44, 50224},  
  {2210501, 3014088, 16, 32, 25112},  
  {3014089, 4043684, 16, 41, 25112},  
  {4043685, 4483148, 32, 35, 12556},  
  {4483149, 4809608, 32, 26, 12556},  
  {4809609, 5098400, 32, 23, 12556}, 
  {5098401, 5349524, 32, 20, 12556},  
  {5349525, 5600648, 32, 20, 12556},  
  {5600649, 5851772, 32, 20, 12556}, 
  {5851773, 6730696, 16, 35, 25112},  
  {6730697, 10849068, 4, 41, 100448}, 
  {10849069,14967440, 4, 41, 100448}, 
  {14967441,14967452, 401792, 8, 1},  
  {14967453,14967464, 401792, 8, 1},  
};

The code below stops on [2] and shows the two errors below. How can I modify this code to make it work?

E2110: Incompatible type conversion (C++) The cast requested can't be done.

E2062 Invalid indirection (C++)

int LoadData( D, iobj, p, v )
double D;
int iobj;
double p[], v[];
{
int s;
//--Lots of code here--

s = objs[iobj][2];

//--more code here--
}
homebase
  • 713
  • 1
  • 6
  • 20

2 Answers2

5
struct ephloc objs[15] = { /* whatever */ };

This defines objs as an array with 15 elements of type ephloc.

objs[iobj]

This accesses an element of that array. Since each element has type ephloc, this expression gives you an ephloc.

objs[iobj][2]

This attempts to access the second element of an array. But objs[iobj] is not an array, so the compiler is telling you that you can't do that.

To access members of that element, use member names:

objs[iobj].first_item_ordinal

This accesses the first member of the object objs[iobj].

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • I believe s=objs[iobj][2]; should be s=objs[iobj].days_per_record; This seems to resolve the compiler problem and return the correct number. – homebase Feb 27 '18 at 01:44
2

This is a case of "C++ is just a superset of C" being proven wrong, again. Your C function declaration is in an outdated from called K&R syntax. Those predates modern function prototypes, and are now invalid in C. Furthermore, they were never valid in C++ to begin with. The only valid function declaration, as far as your ,C++ compiler is concerned, is this:

int LoadData( double D, int iobj, double p[], double v[] )
{

And you better well make sure the corresponding header file matches it. K&R comes hand in hand with the "function without a prototype" declaration, i.e. int LoadDate();, which in C++ is a function taking no arguments. So that will be a declaration mismatch if you don't adjust that too.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458