how to initialize an object declared in header file?

Hello,

working with classes, I am having problems parsing constructor arguments to object I create. Specifically, I am using the class found here: http://www.codeproject.com/KB/cpp/MovingAverages.aspx. It creates objects that I can pass values to, then returns the moving average of those values.

After declaring an object in my .h file, using the standard constructor, so without parsing arguments, the object works like it should. However, I wish to use custom number of values to calculate average from. In the .h file, declaring an object like below gives errors.

CWeightedMovingAverage averager(5);

Errors:

1
2
3
src/testApp.h|76|error: expected identifier before numeric constant|
src/testApp.h|76|error: expected ‘,’ or ‘...’ before numeric constant|
||=== Build finished: 2 errors, 0 warnings ===|


Putting this code in .cpp file works, but it means I cannot use the object, because it will only exist temporarily in one specific method of the main testApp class which is called by main(). I am using openframeworks. The testApp object has methods like update() and draw() that get called sequentially all the time.

How can I parse the constructor argument outside of .h file, after declaring the object? Or, is there another way to construct this object, in .h file? Or should I revise the code so I can have a init() method or so?
Last edited on
Well,
A) you shouldn't use globals anyways
B) it kinda sounds like you are declaring a CWeightedMovingAverage object before the class declaration. Just put it below the class declaration and it should work.
C) Unless someone forces you to do so, leave the "C" part out of the class name. It doesn't add any information, it just makes your code harder to read.
Last edited on
Thanks for your (fast!) reply. I am not sure whether I am using globals, I am quite new to the world of C++. Anyway I need something that will exist throughout all the methods of the testApp class object. So I thought I could use a separate class.

The basic structure is like this:

main.cpp:
1
2
3
4
5
#include testApp.h
int main()
{
   ofRunApp(new testApp());
}


The testApp.h file then includes MovingAverage.h. Inside testApp.h I wish to declare an instance of CWeightedMovingAverage (the C in the name is part of the original files). This instance I want to use to calculate the average of a number that is calculated anew every round of the update() method of testApp. Then I use the average of the last n times update() has calculated a number to draw a circle in a specific place in the draw() method of testApp. These method get called by ofRunApp automatically all the time.

Maybe I should include the whole MovingAgerage class inside the testApp class?
Wait - so you need this for a class? In that case, your instance should probably be a class member. Could you show me the contents of testApp.h? If it's very long, just cut out the parts that don't seem relevant to you.
Since the code is divided over some files (for convenience and keeping track of what is where) the testApp.h is not so long. Below a short version. There are other includes and object that are declared, but so far they do not require settings parsed in their constructor (though I am beginning to see that this may be way more efficient and convenient than a setup() method in some cases). The program tracks motion and color using openCV (which openframeworks provides as an addon).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once

#include "ofMain.h"
#include "ofxOpenCv.h"
#include "MovingAverage.h"
#include <iostream>
#include <limits>

class testApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();
		void exit();

		void keyPressed  (int key);
		etc... (couple more of these)

		ofVideoGrabber 		vidGrabber;     //Ps3eye
                etc...

                int camWidth, camHeight;            //Cam width and height

                //below are the object I wish to initialize with a variable for the constructor, so they do not only have the built in standard setting of the class (running average over last 10 values)
                CWeightedMovingAverage averagerCV;
                CWeightedMovingAverage averagerPix;
                CWeightedMovingAverage movementCircleX;
                CWeightedMovingAverage movementCircleY;

};


Sorry if the question was not so clear from my first post. And thanks for thinking along :)
HAH. Sorry. Uh yeah, you have to do that in the constructor of your class with an initialization list:

1
2
3
4
testApp::testApp() :
averagerCV(whatever_value_you_want)//do the same thing for all of those
{
}


Cool! That worked out of the box :-). After declaring the constructor for testApp I could simply add the initialization list to the implementation of the constructor. Thanks a lot, I really couldn't find a solution to this through google and the like.
Topic archived. No new replies allowed.