printf() trouble...

Hey,
So I've got a question concerning printf(). In an attempt to gain a better understanding of OOP in Workbench 3.0, I found a snippet of code online and have been messing around with it. The only problem is that this particular code utilizes 'cout', unfortunately, I'm constrained to using 'printf()' with Workbench/VxWorks.

Normally this isn't a problem and I feel relatively comfortable using printf() instead of cout. BUT, I've never really dealt with return functions in conjunction with printf()--only voids--and I was hoping someone could help me or at least point me to the right website (all of the ones I've checked have just given me the standard '%' output methods.).

Below is the code, it is made up of a *.h header file and two *.cpp files. One as a main file and the other as a class definition file.

I believe my problem is at the bottom of the "cube.cpp" file:

Here is the header "cube.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CUBE_H_
#define CUBE_H_

class Cube
{
public:
	Cube();
	~Cube();
	void setSide(double s);
	double Cube::getSide();
	double Cube::Area();
	double Cube::Volume();
	void Properties();
private:
	double side;
};
#endif /*CUBE_H_*/ 


Here is the class definition "cube.cpp":
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
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include "cube.h"

Cube::Cube()
{
	
}

Cube::~Cube()
{
	
}

void Cube::setSide(double s)
{
	side = s <= 0 ? 1 : s;
}
	
double Cube::getSide()
{
	return side;
}

double Cube::Area()
{
	return 6 * side * side;
}

double Cube::Volume()
{
	return side * side * side;
}

void Cube::Properties()
{
	printf("Characters of this cube: \n");
	printf("nSide: %d\n", getSide());
	printf("nArea: %d\n\n", Area());
	printf("nVolume: %d\n\n", Volume());
}


Here is the main "start.cpp":
1
2
3
4
5
6
7
8
9
10
11
12
#include "cube.h"

void init()
{
	Cube cube;
	cube.setSide(-12.55);
	cube.Properties();
	
	Cube de;
	de.setSide(28.15);
	de.Properties();
}



This is the expected output:
Characteristics of this cube
Side = 1
Area = 6
Volume = 1

Characteristics of this cube
Side = 28.15
Area = 4754.53
Volume = 22306.7


Any help is greatly appreciated.
Last edited on
The numbers you want to print are doubles, but you are using %d, which is for ints. Try %f.
Ah, of course... thank you Hammurabi! I was foolishly associating the 'd' in '%d' with double.

Switching over to '%f' allows it to Build with no errors or warnings. So that's great.

Unfortunately I am now coming up with linker issues:

Under Workbench I get:

Download completes with unresolved symbols reported!
Please resolve the following symbols before next download:
_Unwind_SjLj_Unregister
_Unwind_SjLj_Register
_Unwind_SjLj_Resume
__gxx_personality_sj0


So I went to try it in Microsoft Visual Studios 2005 (thinking maybe it was just Workbench being weird) and get 2 errors:

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals
Ok, so I took out the destructor from "cube.h" and "cube.cpp" and it all works great in Workbench (building, linking, running, etc).. Microsoft Visual Studio still has problems with it though. The same as mentioned before:

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals


Any thoughts?
You don't have a main function. Rename void init() to be int main() and return 0; at the end of it.
That's awesome. I keep forgetting to switch over my methods of naming initializer functons when going from Workbench to MVS (Workbench doesn't really like it when you use main). I really do appreciate both of your help!

Thanks!
Last edited on
Topic archived. No new replies allowed.