473,779 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unexpected template behavior

I have a template class that works fine when I implement it with <int>,
but when I use <floator <doubleit doesn't work. The class has a
dynamic array of type T that gets instantiated in my constructor. When
type T is int, the array works like I expect. But when I use double or
float, the array points to garbage and any updates to array elements
make no difference.

I'm using Visual Studio 2005 on an XP pro machine. The following is
the code that is giving me problems. If anyone can help explain what
I'm doing wrong and why this doesn't work, I'd really appreciate it.
Thanks in advance.
template <class T>
class Tuple
{
public:
Tuple<T>(void);
Tuple<T>(T x, T y);
~Tuple<T>(void) ;

T getX();
T getY();

void setX(T x);
void setY(T y);
private:
T *_values; //arrray of values
};

template <class T>
Tuple<T>::Tuple (void)
{
_values = new T[2];
_values[0] = 0;
_values[1] = 0;
}

template <class T>
Tuple<T>::Tuple (T x, T y)
{
_values = new T[2];
_values[0] = x;
_values[1] = y;
}

template <class T>
Tuple<T>::~Tupl e(void)
{
}

template <class T>
T Tuple<T>::getX( ) { return _values[0]; }

template <class T>
T Tuple<T>::getY( ) { return _values[1]; }

template <class T>
void Tuple<T>::setX( T x) { _values[0] = x; }

template <class T>
void Tuple<T>::setY( T y) { _values[1] = y; }

Jan 4 '07
11 1629

Noah Roberts wrote:
ho************* *******@gmail.c om wrote:
Tuple<floattup2 (2.2f, 2.5f);
printf("tup2: %d, %d\n", tup2.getX(), tup2.getY()); //prints garbage

Because %d is not the formatting character for floating point numbers.

#include <iostream>
#include <cstdio>
int main(void)
{
double x = 5.5;
printf("%d", x);
std::cin.get();
}

I get 0 but it's totally unpredictable what will happen on any given
computer.
Well... Kai was right, it looks like its your test code... When I was
testing your code I was using std::cout to print out the array
values... you may want to use std::cout instead of printf, and this is
atually a good example of why...

Jan 4 '07 #11
Changing from printf to std::cout solved everything. Sheesh!

Thanks for the help everyone.

Jan 4 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2840
by: will | last post by:
All I am trying to pass a simple xml file using xmlspy : <?xml version="1.0" standalone="no"?> <inventory> <vehicle> <make>Chevy</make>
4
1780
by: Generic Usenet Account | last post by:
I am seeing some unexpected behavior while using the STL "includes" algorithm. I am not sure if this is a bug with the template header files in our STL distribution, or if this is how it should work. For your benefit, let me first quote from the STL Standard Reference (Stepanov & Lee): template <class InputIterator1, class InputIterator2> bool includes(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2...
9
2180
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! http://www.geocities.com/jeff_louie/oop30.htm Regards, Jeff *** Sent via Developersdex http://www.developersdex.com ***
62
3795
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
2
2070
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
9
3472
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
6
1330
by: Kai-Uwe Bux | last post by:
Juha Nieminen wrote: I think your code violates the One-Definition-Rule. The template function foo() is defined in two significantly different ways in the two files. As far as I know, no diagnostic is required for such errors.
3
2028
by: Unknownmat | last post by:
Here is the smallest code I could come up with to reproduce the I was able to reproduce the warning with an even smaller snippet of code: #include <vector> #include <functional> #include <boost/function.hpp> int main()
11
2936
by: JRough | last post by:
I'm trying to use output buffering to cheat so i can print to excel which is called later than this header(). header("Content-type: application/xmsdownload"); header("Content-Disposition: attachment; header("Pragma: no-cache"); header("Expires; 0"); print "$header\n$data";
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.