Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with nested structs

Posted on 2006-07-14
6
Medium Priority
?
263 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: 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.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

671 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