Solved

Problem with nested structs

Posted on 2006-07-14
6
240 Views
Last Modified: 2010-04-24
Hi,

This is my first attempt to use Visual C++. I have code which compiles OK under Unix/Linux (using C) but not under Visual C++. Any ideas on the problem/solution?

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <iostream>  
#include <string>

int sample_test (double value, int test_num)
{
      static struct pat_data {                  // dynamically allocated structure holding PAT values
            int test_num;                              // one struct for each test_num
            int count;                                    // # of tests measured
            double mean;                              // mean value of all tests
            struct test_data *data;                  // individual data for this test_num
            struct test_data *dfirst;
            struct test_data *dlast;
            struct pat_data *next;
      } pat_data_entry;
      static struct pat_data *pfirst=NULL;
      static struct pat_data *plast=NULL;

      static struct test_data {
            double value;
            struct test_data *next;
      } test_data_entry;

      struct pat_data *pdata;
      static double fudge=0;
      double lvalue;

      printf ("Start of PAT with test_num=%d, value=%e\n",test_num,value);

      fudge += 1e-6;
      lvalue = fudge;

      pdata = pfirst;
      while (pdata) {
            if (pdata->test_num == test_num)
                  goto test_num_matched;
            pdata = pdata->next;
      }
      
      pdata = (struct pat_data *)malloc(sizeof(pat_data_entry));
      if (!pdata) {
            //ERROR in allocating space
      }

      pdata->test_num = test_num;
      pdata->count = 0;

      if (!pfirst) {
            pfirst = pdata;
            plast = pdata;
            pdata->next = NULL;
      } else {
            pdata->next = NULL;
            plast->next = pdata;
            plast = pdata;
      }
      pdata->dfirst = NULL;
      pdata->dlast = NULL;

test_num_matched:
// 1st compiler error is on this line:
      pdata->data = (struct test_data *)malloc(sizeof(test_data_entry));
// error is:
// c:\apps\sample_testing.cpp(72) : error C2440: '=' : cannot convert from 'struct sample_test::test_data *' to 'struct test_data *'
//        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
      if (!pdata->data) {
            //ERROR in allocating space
      }

      pdata->data->value = lvalue;
      ++pdata->count;

      if (!pdata->dfirst) {
            pdata->dfirst = pdata->data;
            pdata->dlast = pdata->data;
            pdata->data->next = NULL;
      } else {
            pdata->data->next = NULL;
            pdata->dlast->next = pdata->data;
            pdata->dlast = pdata->data;
      }

      pdata = pfirst;
      while (pdata) {
            printf ("%5d (%4d): ",pdata->test_num,pdata->count);
            pdata->data = pdata->dfirst;
            while (pdata->data) {
                  printf ("%e, ",pdata->data->value);
                  pdata->data = pdata->data->next;
            }
            printf ("\n");
            pdata = pdata->next;
      }
      return (true);
}

jimdgar
0
Comment
Question by:jimdgar
  • 4
  • 2
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 17109647
Hi jimdgar,
>  pdata->data = (struct test_data *)malloc(sizeof(test_data_entry));

In C++, you don't need to use the key word struct when casting.

Try doing it without the keyword in the type cast.

David Maisonave (Axter)
Cheers!
0
 

Author Comment

by:jimdgar
ID: 17109832
David,

That's not it. The same casting works in line 45. I think it has something to do with nesting the structs.

jimdgar
0
 
LVL 30

Expert Comment

by:Axter
ID: 17109868
You need a forward declaration for test_data
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 30

Expert Comment

by:Axter
ID: 17109890
Example:
int sample_test (double value, int test_num)
{
      struct test_data; //Forward declaration .....................*************************
      static struct pat_data {               // dynamically allocated structure holding PAT values
            int test_num;                         // one struct for each test_num
            int count;                              // # of tests measured
            double mean;                         // mean value of all tests
            struct test_data *data;               // individual data for this test_num
            struct test_data *dfirst;
            struct test_data *dlast;
            struct pat_data *next;
      } pat_data_entry;
      static struct pat_data *pfirst=NULL;
      static struct pat_data *plast=NULL;

      static struct test_data {
            double value;
            struct test_data *next;
      } test_data_entry;

      struct pat_data *pdata;
      static double fudge=0;
      double lvalue;

      printf ("Start of PAT with test_num=%d, value=%e\n",test_num,value);

      fudge += 1e-6;
      lvalue = fudge;

      pdata = pfirst;
      while (pdata) {
            if (pdata->test_num == test_num)
                  goto test_num_matched;
            pdata = pdata->next;
      }

      pdata = (pat_data *)malloc(sizeof(pat_data_entry)); //Remove keyword ..........................**********
      if (!pdata) {
            //ERROR in allocating space
      }

      pdata->test_num = test_num;
      pdata->count = 0;

      if (!pfirst) {
            pfirst = pdata;
            plast = pdata;
            pdata->next = NULL;
      } else {
            pdata->next = NULL;
            plast->next = pdata;
            plast = pdata;
      }
      pdata->dfirst = NULL;
      pdata->dlast = NULL;

test_num_matched:
      // 1st compiler error is on this line:
      pdata->data = (test_data *)malloc(sizeof(test_data_entry)); //Remove keyword ..........................**********
      // error is:
      // c:\apps\sample_testing.cpp(72) : error C2440: '=' : cannot convert from 'struct sample_test::test_data *' to 'struct test_data *'
      //        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
      if (!pdata->data) {
            //ERROR in allocating space
      }

      pdata->data->value = lvalue;
      ++pdata->count;

      if (!pdata->dfirst) {
            pdata->dfirst = pdata->data;
            pdata->dlast = pdata->data;
            pdata->data->next = NULL;
      } else {
            pdata->data->next = NULL;
            pdata->dlast->next = pdata->data;
            pdata->dlast = pdata->data;
      }

      pdata = pfirst;
      while (pdata) {
            printf ("%5d (%4d): ",pdata->test_num,pdata->count);
            pdata->data = pdata->dfirst;
            while (pdata->data) {
                  printf ("%e, ",pdata->data->value);
                  pdata->data = pdata->data->next;
            }
            printf ("\n");
            pdata = pdata->next;
      }
      return (true);
}
0
 
LVL 30

Accepted Solution

by:
Axter earned 250 total points
ID: 17109915
Even better, just move the struct.
Example:
int sample_test (double value, int test_num)
{
      static struct test_data {
            double value;
            struct test_data *next;
      } test_data_entry;

      static struct pat_data {               // dynamically allocated structure holding PAT values
            int test_num;                         // one struct for each test_num
            int count;                              // # of tests measured
            double mean;                         // mean value of all tests
            struct test_data *data;               // individual data for this test_num
            struct test_data *dfirst;
            struct test_data *dlast;
            struct pat_data *next;
      } pat_data_entry;
      static struct pat_data *pfirst=NULL;
      static struct pat_data *plast=NULL;

      struct pat_data *pdata;
      static double fudge=0;
      double lvalue;

      printf ("Start of PAT with test_num=%d, value=%e\n",test_num,value);

      fudge += 1e-6;
      lvalue = fudge;

      pdata = pfirst;
      while (pdata) {
            if (pdata->test_num == test_num)
                  goto test_num_matched;
            pdata = pdata->next;
      }

      pdata = (pat_data *)malloc(sizeof(pat_data_entry)); //Remove keyword ..........................**********
      if (!pdata) {
            //ERROR in allocating space
      }

      pdata->test_num = test_num;
      pdata->count = 0;

      if (!pfirst) {
            pfirst = pdata;
            plast = pdata;
            pdata->next = NULL;
      } else {
            pdata->next = NULL;
            plast->next = pdata;
            plast = pdata;
      }
      pdata->dfirst = NULL;
      pdata->dlast = NULL;

test_num_matched:
      // 1st compiler error is on this line:
      pdata->data = (test_data *)malloc(sizeof(test_data_entry)); //Remove keyword ..........................**********
      // error is:
      // c:\apps\sample_testing.cpp(72) : error C2440: '=' : cannot convert from 'struct sample_test::test_data *' to 'struct test_data *'
      //        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
      if (!pdata->data) {
            //ERROR in allocating space
      }

      pdata->data->value = lvalue;
      ++pdata->count;

      if (!pdata->dfirst) {
            pdata->dfirst = pdata->data;
            pdata->dlast = pdata->data;
            pdata->data->next = NULL;
      } else {
            pdata->data->next = NULL;
            pdata->dlast->next = pdata->data;
            pdata->dlast = pdata->data;
      }

      pdata = pfirst;
      while (pdata) {
            printf ("%5d (%4d): ",pdata->test_num,pdata->count);
            pdata->data = pdata->dfirst;
            while (pdata->data) {
                  printf ("%e, ",pdata->data->value);
                  pdata->data = pdata->data->next;
            }
            printf ("\n");
            pdata = pdata->next;
      }
      return (true);
}
0
 

Author Comment

by:jimdgar
ID: 17110143
Duh, I should have known that.

Thanks, David.

jimdgar
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now