I'm taking an intro course to C++ and we're covering stack overload and how the library and linker come into play with programming.

I have a simple .cpp file called dataType.cpp. The code is as follows

int A;
int rtn;

int main()
{
rtn = dataType(A);
cout << rtn << "\n";
return rtn;
};

we're supposed to write a function that returns an integer. If the parameter is a long double return a value of 1, if its a double return a value of 2, a float should return a value of 3, an unsigned long integer should return a value of 4, long integer 5, unsigned integer 6, integer 7, unsigned short integer 8 etc.

Now......I believe that the header file is basically my function. But if my .cpp says to simply return a value - i guess I'm not sure what my header should say?? To me it seems as if the function already exists in the .cpp file?

do i need to declare the long integer, double, integer, float etc.? I have no clue what i should be puttin g in this header file and would appreciate any help

what i want to put in the header file is something like this

int (long double dataIn);

for each of my data types. But i don't feel like i'm on the right track.

To me, this looks like the function because the variables are declared and then we go into the main function inside the braces. I am going to display on the screen the return value of dataType(A).

Your function declaration(s) (hint there) will go into your header file. So like what you have in your second code box but with a name of the function. Think over how you're going to determine which is which.

You don't need to declare your variables outside of main() like that.

Also, on your first code block you need an = between code and C (or CODE=C++ works too).

You don't need a user defined header file for this program unless you make two .cpp files.

hint #2- overloaded

Your function declaration(s) (hint there) will go into your header file. So like what you have in your second code box but with a name of the function. Think over how you're going to determine which is which.

You don't need to declare your variables outside of main() like that.

Also, on your first code block you need an = between code and C (or CODE=C++ works too).

yeah, that's where i seem to be having the confusion. isn't the main() in the .cpp file the function?
so would i indicate each of the datatypes for main? like int main(); or float main(); ?? sorry if my questions are silly, this is new to me, but i am doing my best to figure it out. The assignment is due tomorrow and i've spent a week trying to figure it out. :(

You don't need a user defined header file for this program unless you make two .cpp files.

hint #2- overloaded

really? 2 .cpp files? even for such a short program? Ok now i'm even more confused. Do i need a separate .cpp file for main()? but then again, i'm still not sure what function i'm putting in the header.
ARRGHH! i know this shouldnt be soo complicated but i'm just hitting a wall here

.cpp files? even for such a short program?

As far as I understood, this seems to be partly about having multiple files in your project, hence two .cpp files plus a header.

Have yourself main.cpp, which comes with the main() function implementation and #includes the header file (.h). The header file declares the functions that you end up writing in the third file, which is the other .cpp file.

Then figure out, how to put these files together in order to compile and link a working executable. If you are allowed to use an IDE, it should be quite easy, if not, it might be little more trickier.

ARRGHH!

LOL, relax.

[EDIT] [B]int [/B]main() is a valid signature for main() , just stick with that in this assignment.

really? 2 .cpp files? even for such a short program? Ok now i'm even more confused. Do i need a separate .cpp file for main()? but then again, i'm still not sure what function i'm putting in the header.
ARRGHH! i know this shouldnt be soo complicated but i'm just hitting a wall here

As far as I understood, this seems to be partly about having multiple files in your project, hence two .cpp files plus a header.

Where are you getting this? I said
"You don't need a user defined header file for this program unless you make two .cpp files. "

I did Not NOT *NOT* say "you need to make 2 .cpp files." main() is only and int function. You need to "write a function" that main() calls that returns an integer, another that returns float, etc. Look at my hint#2 and stop being confused by what's written here and read what's in your book when we point out hints.

Where are you getting this?

Help w/ what goes in c++ header file

I would assume that there is at least one header file,
most likely including function declarations for this assignment.

... and how the library and linker come into play with programming.

To me this implies that there would be more than one .cpp file
involved, demonstrating how e.g. two object files are used to produce an executable (unless this assignment is the first step to that, i.e. with one .h + one .cpp, which will be expanded upon later on)

@OP
Could you provide somewhat more detailed general description about this assignment?

The themarval is blowing smoke. He seems to be asking completely unrelated questions to the task. Header files is a red herring and confusing everyone. Let's dissect the original post:

I'm taking an intro course to C++ and we're covering stack overload and how the library and linker come into play with programming.

What they learned in class. Does this in fact relate to the question or just fluff information?

I have a simple .cpp file called dataType.cpp. The code is as follows

int A;
int rtn;

int main()
{
rtn = dataType(A);
cout << rtn << "\n";
return rtn;
};

we're supposed to write a function that returns an integer. If the parameter is a long double return a value of 1, if its a double return a value of 2, a float should return a value of 3, an unsigned long integer should return a value of 4, long integer 5, unsigned integer 6, integer 7, unsigned short integer 8 etc.

There is the problem definition. Nothing about header files, linkers. just a straight forward programming problem about overloading a function.


Now the red herring starts:

Now......I believe that the header file is basically my function. But if my .cpp says to simply return a value - i guess I'm not sure what my header should say?? To me it seems as if the function already exists in the .cpp file?

Header file is a function? Obviously themarval doesn't really know what a header file does. Does this comment relate to the problem above? No.

do i need to declare the long integer, double, integer, float etc.? I have no clue what i should be puttin g in this header file and would appreciate any help

what i want to put in the header file is something like this

int (long double dataIn);

for each of my data types. But i don't feel like i'm on the right track.

More smoke. He's locked on this header file thing like a bulldog and is missing the point of the project -- overloaded functions.

Nowhere does the problem state headers, multiple C++ source files, compilers, linkers. They are all mentioned simply because they talked about them in class. Get rid of all that baggage and concentrate on the problem:

we're supposed to write a function that returns an integer. If the parameter is a long double return a value of 1, if its a double return a value of 2, a float should return a value of 3, an unsigned long integer should return a value of 4, long integer 5, unsigned integer 6, integer 7, unsigned short integer 8 etc.

good points

Oh well, the OP is at the moment quite confused about pretty much everything, but let's just wait and see if he returns and is able to tell what this is all about (I'm still not ruling out that linker/object file- 'thing' ...).

ok fair enough I understand what you mean with the whole red herring rant. Perhaps my lack of understanding of what a header does impeded my ability to ask the question clearly. I do believe at this point that i know the true purpose of a header and i have taken this code a bit farther but I am now getting compile errors.

So my clear question would be - can you please review this code and suggest to me where i am making an error? I have googled, and searched various forums to resolve this but no luck.

So here is my header file called dataType.h

#define dataType_h

int dataType(long double dataIn);
int dataType(double dataIn);
int dataType(float dataIn);
int dataType(unsigned long dataIn);
int dataType(long dataIn);

and here is my dataType.cpp

#include <iostream>
#include "c:\users\veemoney\csc263\dataType.h"
using namespace::std;

int (A);		
int rtn;	

int main()
{
	rtn = dataType(A);
	cout << rtn << "\n";
	return rtn;
};

int dataType(long double dataIn)
{
	rtn = dataType(1);
	cout << rtn << "\n";
	return rtn;
};

int dataType(double)
{
 	rtn = dataType(2);
	cout << rtn << "\n";
	return rtn;
};


int dataType (float dataIn)
{
 	rtn = dataType(3);
	cout << rtn << "\n";
	return rtn;
};


int dataType(unsigned long dataIn)
{
 	rtn = dataType(4);
	cout << rtn << "\n";
	return rtn;
};

int dataType(long dataIn)
{
	retn = dataType(5);
	cout << rtn << "\n";
	return rtn;
}:

The errrors are plenty........
datatype.cpp(10) : error C2668: 'dataType' : ambiguous call to overloaded function
datatype.h(7): could be 'int dataType(long)'
datatype.h(6): or 'int dataType(unsigned long)
datatype.h(5): or 'int dataType(float)'
datatype.h(4): or 'int dataType(double)'
datatype.h(3): or 'int dataType(long double)'
1> while trying to match the argument list '(int)'

The same errors exist for the dataType.cpp file

The problem there is that A is not assigned any value before putting it into the method. Take it out of global space (there's no reason to have it there) and put it in main with a value.

You don't have to call datatype from within your datatype functions. The key here is that the compiler makes the decision for you as to which one to call. Once it's got the type of the variable (though there could be some promotion going on but let's assume that away for now) it picks the method.

I think you're right in having your methods return the same type of variable but what kind of information can an int give you? What if your methods could speak, well in a way they can...

Ok. Have a bit of patience with me here - i'm totally newbie at this.
I get what you're saying about removing the 'A' argument and i've removed that. When you say that i dont have to call my datatype i'm not sure what other way to do it. This is how we've been taught (at least up til this point)

each parameter type, be it long, double, float, unsigned long integer etc, is supposed to return a specified integer.
if the parameter is an integer type it should return lets say a 1,
if the parameter is an float type it should return lets say a 2, a character would return a 5 etc.

I've looked at this so long i can't even see straight.

Ok. Have a bit of patience with me here - i'm totally newbie at this.
I get what you're saying about removing the 'A' argument and i've removed that. When you say that i dont have to call my datatype i'm not sure what other way to do it. This is how we've been taught (at least up til this point)

I have patience don't worry. You get close to it and then you take a detour, hehe. I meant take line 5 and move it in between lines 9 and 10. Leave your call on line 10 alone. I meant on line 17,24 etc you are recursively calling your function for some reason. There's no need to -- once you're in the method you know that you have passed that type of variable to it.

each parameter type, be it long, double, float, unsigned long integer etc, is supposed to return a specified integer.
if the parameter is an integer type it should return lets say a 1,
if the parameter is an float type it should return lets say a 2, a character would return a 5 etc.

I figured that out after I wrote my reply. I was alluding to using a string but this is your requirement, I understand. Well this one you can do simply by returning the number you want. If you're in the long double method, just return 1. No need for the newline or anything.

thank you for being patient. i know that the learning curve can sometimes be frustrating for teh person who's trying to help just as much as the person whos asking for it. Because this is due in a matter of hours - ive been working on it since my last post and i've made some revisions that may be in line with what you've recently posted.
header file

#define dataType_h

int dataType(long double dataIn);
int dataType(double dataIn);
int dataType(float dataIn);
int dataType(unsigned long int dataIn);
int dataType(long int dataIn);
int dataType(unsigned int dataIn);
int dataType(int dataIn);
int dataType(unsigned short int dtaaIn);
int dataType(short int dataIn);
int dataType(unsigned char);
int dataType(char);

cpp file

#include <iostream>
#include "c:\users\veemoney\csc263\dataType.h"
using namespace::std;

i//nt A;		
int rtn;	

int main()
{
	rtn = 0;
	cout << rtn << "\n";
	return rtn;
};

int dataType(long double dataInt)
{
 	rtn = 1;
	cout << rtn << "\n";
	return rtn;
};

int dataType(double dataInt)
{
 	rtn = 2;
	cout << rtn << "\n";
	return rtn;
};


int dataType (float dataIn)
{
 	rtn = 3;
	cout << rtn << "\n";
	return rtn;
};


int dataType(unsigned long dataIn)
{
 	rtn = 4;
	cout << rtn << "\n";
	return rtn;
};


int dataType(long dataIn)
{
	retn = 5;
	cout << rtn << "\n";
	return rtn;
}:

gives me less errors but i dont kinow if i'm getting any closer

error C2086: 'int rtn' : redefinition
datatype.h(15) : see declaration of 'rtn'
datatype.cpp(48) : error C2065: 'retn' : undeclared identifier
datatype.cpp(51) : error C2059: syntax error : ':'

i walked through my errors from the last post that i sent you and i was able to resolve all of them. My code compiled and my solution was built successfully. Now i just need to run the linker succesfully and i may be there. i'm glad i was able to figure it out with some determination but i definately appreciate your suggestions as well!!!
thank you!!!
Now i just need to try and build my library. Wish me luck!

please review my code to let me know what I'm doing wrong here.
I'm building a function that will return a specific value if the parameter is of a specific type.

How would i proceed with assigning a specific return value for each parameter type?

I've managed to compile and build my library I just need to figure out this one piece. Any help would be appreciaetd

This is the beginning of my .h file.

i thought i could just put
int dataType(long double dataIn) = 1 but that doesn't work.

int dataType(long double dataIn);
int dataType(double dataIn);
int dataType(float dataIn);
int dataType(unsigned long int dataIn);
int dataType(long int dataIn);
int dataType(unsigned int dataIn);
int dataType(int dataIn);
int dataType(unsigned short int dtaaIn);
int dataType(short int dataIn);
int dataType(unsigned char);
int dataType(char);

please review my code to let me know what I'm doing wrong here.
I'm building a function that will return a specific value if the parameter is of a specific type.

How would i proceed with assigning a specific return value for each parameter type?

I've managed to compile and build my library I just need to figure out this one piece. Any help would be appreciaetd

This is the beginning of my .h file.

i thought i could just put
int dataType(long double dataIn) = 1 but that doesn't work.

int dataType(long double dataIn);
int dataType(double dataIn);
int dataType(float dataIn);
int dataType(unsigned long int dataIn);
int dataType(long int dataIn);
int dataType(unsigned int dataIn);
int dataType(int dataIn);
int dataType(unsigned short int dtaaIn);
int dataType(short int dataIn);
int dataType(unsigned char);
int dataType(char);

Something like this?

int dataType(long double dataIn)
{
   return 0;
}

int dataType(double dataIn)
{
   return 1;
}

int dataType(float dataIn)
{
   return 2;
}

hmm, i would imagine that would work. I'm curious though, when you're using an equal sign in c+ doesn't that assign a value to the object or am i just confused?

Something like this?

int dataType(long double dataIn)
{
   return 0;
}

int dataType(double dataIn)
{
   return 1;
}

int dataType(float dataIn)
{
   return 2;
}

thanks for your suggestion but it generates compile errors on the header file.

I'm curious though, when you're using an equal sign in c+ doesn't that assign a value to the object or am i just confused?

Well, if it is a primitive data type like double, bool, int, etc., yes.

double a;
bool b;
int c;

a = 4.5;
b = true;
c = 9;

If it's something where an assignment operator (=) has been written, like the string class, yes. But you're dealing with functions, so it's different. You can assign a variable to the RETURN value of a function:

int foo {}

int a = foo ();  // = operator

thanks for your suggestion but it generates compile errors on the header file.

Impossible to comment without seeing more code or at least the error statements.

you were right, the only thing that was missing was the ( ) or ' ' around the numeric values.
I resolved it.
thank you for your help

Not to beat a dead horse (or a turned-in assignment) but where were you putting the () and the ' ' ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.