Solved

Problem with nested structs

Posted on 2006-07-14
6
236 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

16 Experts available now in Live!

Get 1:1 Help Now