Solved

Problem with nested structs

Posted on 2006-07-14
6
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
How do I write a C++ program (coursework) 20 148
How do I set cell to blank in a Combo box column at a datagridview 3 106
VC++ 6.0 issue 3 82
Need help with a query 7 58
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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