?
Solved

beginner: get string/int/float from other class

Posted on 2000-04-27
7
Medium Priority
?
184 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Accepted Solution

by:
Zoppo earned 120 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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