Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with nested structs

Posted on 2006-07-14
6
Medium Priority
?
266 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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

783 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