Solved

beginner: get string/int/float from other class

Posted on 2000-04-27
7
177 Views
Last Modified: 2010-04-02
win32/VC++6/beginner/stuck

in class
Chart2.ccp :
float ppdata [] = {
003.0f, 096.9f, 031.7f, 086.5f,
101.2f, 069.8f, 210.6f, 34.5f,
089.4f, 086.5f, 077.9f, 069.6f,
095.1f, 015.8f, 084.5f, 100.7f
};


in class w1Dlg.cpp
big need of those floats.
trying in w1Dlg.cpp:
float newppdata;
newppdata = chart2.ppdata[0];
but compiler is saying that ppdata is no element of CChart2.

if you want to help me please write some lines of accessing other classes string/int/float

thanks
0
Comment
Question by:peter_b
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:GlennDean
ID: 2757328
Some clarification questions:
Do you have a something like
class Chart2
{
float ppdata[];
....
};
 
Somewhere in your code you instantiate an object like
Chart2 chart2;

Then you want to access chart2.ppdata[0];

   Glenn
0
 
LVL 22

Expert Comment

by:nietod
ID: 2757634
We need to see your real code. and the exact error message.  You shoudl be able to copy and paste them into the text box.
0
 

Author Comment

by:peter_b
ID: 2758312
w1dlg.cpp:

void CW1Dlg::SaveProfile()
{
      CWinApp* app = AfxGetApp();
      app->WriteProfileInt("profile", "COM", comCombo.GetCurSel());
      app->WriteProfileInt("profile", "phone", phoneCombo.GetCurSel());
      app->WriteProfileInt("profile", "pulse", pulseCheck.GetCheck() & 1);
      app->WriteProfileString("profile", "prefix",m_strMessage);



      char newdata[6];
      float newppdata;
      for (int i = 0; i < 15; i++)
      {
            newppdata = chart2.ppdata[i];
            sprintf(newdata,"%f",ppdata[i]);
            app->WriteProfileString("profile","winds[i]", newdata);
            printf(newdata);
      }
      


}


chart2.cpp

// Chart2.cpp : implementation file
//

#include "stdafx.h"
#include "w1.h"
#include "Chart2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChart2

CChart2::CChart2()
{
      yAxisTicks = 15;
      X1 = 40;
      X2 = 520;
      Y1 = 40;
      Y2 = 320;
      height;
      arrowMainOffset = 3;
      arrowMidOffset = 5;
      lineWidth = 4;
      maxValue = 0;
      
}

CChart2::~CChart2()
{
}


BEGIN_MESSAGE_MAP(CChart2, CStatic)
      //{{AFX_MSG_MAP(CChart2)
      ON_WM_PAINT()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

float ppdata [] = {
003.0f, 096.9f, 031.7f, 086.5f,
101.2f, 069.8f, 210.6f, 34.5f,
089.4f, 086.5f, 077.9f, 069.6f,
095.1f, 015.8f, 084.5f, 100.7f
};
....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Accepted Solution

by:
Zoppo earned 30 total points
ID: 2758768
Hi peter_b,

you declared the ppdata as a global variable, not as member of a class. Now we just would need to know what you need this for (do you want to use this array for all instances of the class or one array for each instantiated object of the class)

If you need only one array for all instances of the class you should declare it as static member of the class like this:

// in .h file
class CChart2
{
 ...
 static float ppdata[];
 ...
}

// in .cpp file
float CChart2::ppdata [] = {
         003.0f, 096.9f, 031.7f, 086.5f,
         101.2f, 069.8f, 210.6f, 34.5f,
         089.4f, 086.5f, 077.9f, 069.6f,
         095.1f, 015.8f, 084.5f, 100.7f
         };

so you can access it in two way like this:

void CW1Dlg::SaveProfile()
{
....
for (int i = 0; i < 15; i++)
{
 newppdata = chart2.ppdata[i]; // via the object
 // or
 newppdata = CChart2::ppdate[i]; // via the class
....
}
....
}


If you need one array for each instantiated object of this class the way to do it depends on what exactly you need. You can either use fixed-sized or dynamic arrays.


At last, as your code implies you need only one global array, there may be no need to hold ppdata as a member. Just declare it as you did as global (at file scope) declared array and declare it as extern wherever you need it like this:

// in chart2.cpp
float ppdata [] = {
         003.0f, 096.9f, 031.7f, 086.5f,
         101.2f, 069.8f, 210.6f, 34.5f,
         089.4f, 086.5f, 077.9f, 069.6f,
         095.1f, 015.8f, 084.5f, 100.7f
         };

// in w1dlg.cpp:
extern float ppdata[];
void CW1Dlg::SaveProfile()
{
 ...
 for (int i = 0; i < 15; i++)
 {
  newppdata = ppdata[i];
  ...
 }
 ...
}


hope that helps,

ZOPPO
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2759220
ZOPPO's right on all counts!
0
 

Author Comment

by:peter_b
ID: 2759391
thanks !
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2759401
you're welcome...

thanks for compliment, GlennDean...

regards,

ZOPPO
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 77
How to convert MFC APP to Win32 APP. 19 112
How to print into std::string like sprintf using C++11? 12 124
boost::uuid crashes 17 41
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

730 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