I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do? 8 2202
* Hayrola:
I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
'typedef' just introduces a new name for a type. The two function
definitions above therefore have the same signature. They're different
definitions of the same function.
'struct', 'class' and 'enum' introduce new types.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"Hayrola" <Ni**********@home.sewrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
>I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
Basically, you are trying to do two different things with the same type
(int). One way would be to have 2 different functions, one with the
VIPfunction, one without.
Another way would be to wrap A and B in a structure or class. Then you
could make a func template and specialize for type B (which would be a
structure).
The questionn becomes, they are both ints. Calling one A and one B in a
typedef doesn't mean much. If you need to differentiate between them then
just keep them straight yourself somehow.
I can see what you are trying to accomplish, but the question is why? How
does this help your pogram? Depending on what you are actually trying to
achieve would say how it could be done.
On 1 Apr, 06:00, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Hayrola" <Niklas_Ha...@home.sewrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
Basically, you are trying to do two different things with the same type
(int). One way would be to have 2 different functions, one with the
VIPfunction, one without.
Another way would be to wrap A and B in a structure or class. Then you
could make a func template and specialize for type B (which would be a
structure).
The questionn becomes, they are both ints. Calling one A and one B in a
typedef doesn't mean much. If you need to differentiate between them then
just keep them straight yourself somehow.
I can see what you are trying to accomplish, but the question is why? How
does this help your pogram? Depending on what you are actually trying to
achieve would say how it could be done.
To clarify what i am doing, A represents a row number, and B
represents a column number.
In the good ol' game chess, rows have numbers (1234...), and columns
have letters (ABCD...).
Therefore the need to have them use different functons.
The already existing (C style) code assumes that A and B are ints, and
uses memsets and memcopys everywhere.
A solution has sprung into my mind, but i dont know if its a very good
one:
void func2(A val1, B val2 = -255)
{
if(val2 == -255)
{
VIPfunc();
std::cout << val1 << std::endl;
}
else
{
std::cout << val2 << std::endl;
}
}
any comments on that?
"Hayrola" <Ni**********@home.sewrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
On 1 Apr, 06:00, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Hayrola" <Niklas_Ha...@home.sewrote in message
news:11**********************@y66g2000hsf.googleg roups.com...
>I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
Basically, you are trying to do two different things with the same type (int). One way would be to have 2 different functions, one with the VIPfunction, one without.
Another way would be to wrap A and B in a structure or class. Then you could make a func template and specialize for type B (which would be a structure).
The questionn becomes, they are both ints. Calling one A and one B in a typedef doesn't mean much. If you need to differentiate between them then just keep them straight yourself somehow.
I can see what you are trying to accomplish, but the question is why? How does this help your pogram? Depending on what you are actually trying to achieve would say how it could be done.
To clarify what i am doing, A represents a row number, and B
represents a column number.
In the good ol' game chess, rows have numbers (1234...), and columns
have letters (ABCD...).
Therefore the need to have them use different functons.
The already existing (C style) code assumes that A and B are ints, and
uses memsets and memcopys everywhere.
A solution has sprung into my mind, but i dont know if its a very good
one:
void func2(A val1, B val2 = -255)
{
if(val2 == -255)
{
VIPfunc();
std::cout << val1 << std::endl;
}
else
{
std::cout << val2 << std::endl;
}
}
any comments on that?
In chess rows go from 1 to 8. Colums go from A to H. No other values are
legitimate.
void func2(int Val )
{
if ( Val >=1 && Val <= 8 )
// I have a row
else if ( Val >= 'A' && Val <= 'H' )
// I have a column
else
// Danger Will Roberson!
}
On 1 Apr, 08:24, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Hayrola" <Niklas_Ha...@home.sewrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
On 1 Apr, 06:00, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Hayrola" <Niklas_Ha...@home.sewrote in message
>news:11**********************@y66g2000hsf.googleg roups.com...
I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
Basically, you are trying to do two different things with the same type
(int). One way would be to have 2 different functions, one with the
VIPfunction, one without.
Another way would be to wrap A and B in a structure or class. Then you
could make a func template and specialize for type B (which would be a
structure).
The questionn becomes, they are both ints. Calling one A and one B in a
typedef doesn't mean much. If you need to differentiate between them
then
just keep them straight yourself somehow.
I can see what you are trying to accomplish, but the question is why?
How
does this help your pogram? Depending on what you are actually trying to
achieve would say how it could be done.
To clarify what i am doing, A represents a row number, and B
represents a column number.
In the good ol' game chess, rows have numbers (1234...), and columns
have letters (ABCD...).
Therefore the need to have them use different functons.
The already existing (C style) code assumes that A and B are ints, and
uses memsets and memcopys everywhere.
A solution has sprung into my mind, but i dont know if its a very good
one:
void func2(A val1, B val2 = -255)
{
if(val2 == -255)
{
VIPfunc();
std::cout << val1 << std::endl;
}
else
{
std::cout << val2 << std::endl;
}
}
any comments on that?
In chess rows go from 1 to 8. Colums go from A to H. No other values are
legitimate.
void func2(int Val )
{
if ( Val >=1 && Val <= 8 )
// I have a row
else if ( Val >= 'A' && Val <= 'H' )
// I have a column
else
// Danger Will Roberson!
}
There is just a minor problem... Both columns and rows are stored in
the format 0 to 7.
this so that they could be used like this: "theBoard[row][col]"
:(
On 2007-04-01 15:51, Hayrola wrote:
On 1 Apr, 06:00, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Hayrola" <Niklas_Ha...@home.sewrote in message
news:11**********************@y66g2000hsf.googleg roups.com...
>I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
Basically, you are trying to do two different things with the same type (int). One way would be to have 2 different functions, one with the VIPfunction, one without.
Another way would be to wrap A and B in a structure or class. Then you could make a func template and specialize for type B (which would be a structure).
The questionn becomes, they are both ints. Calling one A and one B in a typedef doesn't mean much. If you need to differentiate between them then just keep them straight yourself somehow.
I can see what you are trying to accomplish, but the question is why? How does this help your pogram? Depending on what you are actually trying to achieve would say how it could be done.
To clarify what i am doing, A represents a row number, and B
represents a column number.
In the good ol' game chess, rows have numbers (1234...), and columns
have letters (ABCD...).
Therefore the need to have them use different functons.
The already existing (C style) code assumes that A and B are ints, and
uses memsets and memcopys everywhere.
A solution has sprung into my mind, but i dont know if its a very good
one:
void func2(A val1, B val2 = -255)
This implies to me that you know at the time you make the call whether
you have a row or a column, so why not simply make two different
functions with different names?
--
Erik Wikström
"Hayrola" <Ni**********@home.sewrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
On 1 Apr, 08:24, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Hayrola" <Niklas_Ha...@home.sewrote in message
news:11**********************@b75g2000hsg.googleg roups.com...
On 1 Apr, 06:00, "Jim Langston" <tazmas...@rocketmail.comwrote: "Hayrola" <Niklas_Ha...@home.sewrote in message
>>news:11**********************@y66g2000hsf.google groups.com...
>I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing.
eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
>Basically, you are trying to do two different things with the same type (int). One way would be to have 2 different functions, one with the VIPfunction, one without.
>Another way would be to wrap A and B in a structure or class. Then you could make a func template and specialize for type B (which would be a structure).
>The questionn becomes, they are both ints. Calling one A and one B in a typedef doesn't mean much. If you need to differentiate between them then just keep them straight yourself somehow.
>I can see what you are trying to accomplish, but the question is why? How does this help your pogram? Depending on what you are actually trying to achieve would say how it could be done.
To clarify what i am doing, A represents a row number, and B
represents a column number.
In the good ol' game chess, rows have numbers (1234...), and columns
have letters (ABCD...).
Therefore the need to have them use different functons.
The already existing (C style) code assumes that A and B are ints, and
uses memsets and memcopys everywhere.
A solution has sprung into my mind, but i dont know if its a very good
one:
void func2(A val1, B val2 = -255)
{
if(val2 == -255)
{
VIPfunc();
std::cout << val1 << std::endl;
}
else
{
std::cout << val2 << std::endl;
}
}
any comments on that?
In chess rows go from 1 to 8. Colums go from A to H. No other values are legitimate.
void func2(int Val ) { if ( Val >=1 && Val <= 8 ) // I have a row else if ( Val >= 'A' && Val <= 'H' ) // I have a column else // Danger Will Roberson!
}
There is just a minor problem... Both columns and rows are stored in
the format 0 to 7.
this so that they could be used like this: "theBoard[row][col]"
Then you need to either make two different functions, pass another parm
stating what the variable type is, or modify the parm to say.
The first method is rather obvoiu.
void funcRow2( int Val );
void funcCol2( int Val );
The second method I would just pass a bool
void func2( int Val, bool Row )
{
if ( Row )
// working with row
else
// working with column
}
The third method is sloppy and kludge.
void func2( int Val )
{
if ( Val 255 )
{
// Dealing with Row
Val -= 255;
}
else
// Dealing with Column
}
Would be used like:
int Row, Col;
cin >Row >Col;
func2( Row + 255 );
func2( Col );
I am not suggesting you use the 2nd method, it's quite ugly and a kludge.
But it is possible.
Your last alternative would work, but I'd probably assign it a value of -1
instead of -255.
Overall, the best method if possible is to preprocess anythign you have to
for row if you can.
void Func2Row( int Val )
{
VIPfunc();
Func2( Val );
}
void Func2Col( int Val )
{
Func2( Val );
}
void Func2( int Val )
{
// whatever
}
I still think the best solution would be to have one function for Row, and
one function for Column, easiest to maintain.
On 1 Apr, 13:21, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Hayrola" <Niklas_Ha...@home.sewrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
On 1 Apr, 08:24, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Hayrola" <Niklas_Ha...@home.sewrote in message
>news:11**********************@b75g2000hsg.googleg roups.com...
On 1 Apr, 06:00, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Hayrola" <Niklas_Ha...@home.sewrote in message
>news:11**********************@y66g2000hsf.googleg roups.com...
I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing.
eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
}
void func(B val)
{ //Error points to this line
std::cout << val << std::endl;
}
This gives me the following error: "function 'void func(A)' already
has a body"
Is there an easy way to solve this, or
is there another way to accomplish what i am trying to do?
Basically, you are trying to do two different things with the same
type
(int). One way would be to have 2 different functions, one with the
VIPfunction, one without.
Another way would be to wrap A and B in a structure or class. Then
you
could make a func template and specialize for type B (which would be a
structure).
The questionn becomes, they are both ints. Calling one A and one B in
a
typedef doesn't mean much. If you need to differentiate between them
then
just keep them straight yourself somehow.
I can see what you are trying to accomplish, but the question is why?
How
does this help your pogram? Depending on what you are actually trying
to
achieve would say how it could be done.
To clarify what i am doing, A represents a row number, and B
represents a column number.
In the good ol' game chess, rows have numbers (1234...), and columns
have letters (ABCD...).
Therefore the need to have them use different functons.
The already existing (C style) code assumes that A and B are ints, and
uses memsets and memcopys everywhere.
A solution has sprung into my mind, but i dont know if its a very good
one:
void func2(A val1, B val2 = -255)
{
if(val2 == -255)
{
VIPfunc();
std::cout << val1 << std::endl;
}
else
{
std::cout << val2 << std::endl;
}
}
any comments on that?
In chess rows go from 1 to 8. Colums go from A to H. No other values
are
legitimate.
void func2(int Val )
{
if ( Val >=1 && Val <= 8 )
// I have a row
else if ( Val >= 'A' && Val <= 'H' )
// I have a column
else
// Danger Will Roberson!
}
There is just a minor problem... Both columns and rows are stored in
the format 0 to 7.
this so that they could be used like this: "theBoard[row][col]"
Then you need to either make two different functions, pass another parm
stating what the variable type is, or modify the parm to say.
The first method is rather obvoiu.
void funcRow2( int Val );
void funcCol2( int Val );
The second method I would just pass a bool
void func2( int Val, bool Row )
{
if ( Row )
// working with row
else
// working with column
}
The third method is sloppy and kludge.
void func2( int Val )
{
if ( Val 255 )
{
// Dealing with Row
Val -= 255;
}
else
// Dealing with Column
}
Would be used like:
int Row, Col;
cin >Row >Col;
func2( Row + 255 );
func2( Col );
I am not suggesting you use the 2nd method, it's quite ugly and a kludge.
But it is possible.
Your last alternative would work, but I'd probably assign it a value of -1
instead of -255.
Overall, the best method if possible is to preprocess anythign you have to
for row if you can.
void Func2Row( int Val )
{
VIPfunc();
Func2( Val );
}
void Func2Col( int Val )
{
Func2( Val );
}
void Func2( int Val )
{
// whatever
}
I still think the best solution would be to have one function for Row, and
one function for Column, easiest to maintain.
Yes, i tink il'l go for two differently named functions.
Thanks Jim and Erik for great answers! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Doran_Dermot |
last post by:
Hi All,
I've seen lots of code in which the attributes of a class are accessed and
modified using two separate methods. For example:
class Problems:
def __init__( self, refNum ):...
|
by: Kamran |
last post by:
Hi
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.
I know that I should direct the question to the relevant
mailing list and I have done that but...
|
by: zeus |
last post by:
I know function overloading is not supported in C.
I have a few questions about this:
1. Why? is it from technical reasons? if so, which?
2. why wasn't it introduced to the ANSI?
3. Is there any...
|
by: lordkain |
last post by:
is it possible to do some kind of function overloading in c? and that
the return type is different
|
by: Pramod |
last post by:
I have one question. Can I catch exception in function overloading.In
my programm i want to create two function with same signature and same
return type. I know its not possible...can i use...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |