It is currently Sat May 27, 2017 8:42 am

All times are UTC - 5 hours




 Page 1 of 1 [ 2 posts ] 
Author Message
 Post subject: A place for c++ Macros: Either Ask about one or submit one!
PostPosted: Thu Jun 23, 2011 10:57 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
I am working on a fairly straight forward macro, but im having trouble to get it to work right.
Here is my sample code.

This is the macro
#ifndef SIGN
#define SIGN(p)  { if(p){ ((p) > 0 ){ return "POSITIVE"; }}\
                    else if(p){ ((p) < 0 ){ return "NEGATIVE"; }}\
                         else { return "ZERO"; } }
#endif


This is my main functions calling the macro
#include "cCell.h"

using namespace std;

int main()
{
   
   string str = SIGN(2);

   cout << "Result is : " << str;
   cout << endl;

   return 0;
}


Here is the compiler code I am receiving:
1>------ Build started: Project: Testing, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2552: 'str' : non-aggregates cannot be initialized with initializer list
1> 'std::basic_string<_Elem,_Traits,_Ax>' : Types with a base are not aggregate
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2143: syntax error : missing '}' before 'if'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2143: syntax error : missing ';' before 'if'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2143: syntax error : missing ';' before '{'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2440: 'return' : cannot convert from 'const char [9]' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2143: syntax error : missing ';' before '{'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2440: 'return' : cannot convert from 'const char [9]' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(8): error C2440: 'return' : cannot convert from 'const char [5]' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(10): error C2143: syntax error : missing ';' before '<<'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(11): error C2143: syntax error : missing ';' before '<<'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(11): error C2086: 'int cout' : redefinition
1> c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(10) : see declaration of 'cout'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(13): error C2059: syntax error : 'return'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(14): error C2059: syntax error : '}'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(14): error C2143: syntax error : missing ';' before '}'
1>c:\users\skilz80\documents\visual studio 2010\projects\testing\testing\main.cpp(14): error C2059: syntax error : '}'
1> Core.cpp
1> cCell.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I am trying to pass any number into the macro, and return a string weather it it postive negative or zero. My next step of a macro would be to do the same exact thing except return -1,0,1 Any help or comments would be appreciated! Thank You in advance <3


Offline
 Profile  
 
 Post subject: Re: A place for c++ Macros: Either Ask about one or submit o
PostPosted: Fri Jun 24, 2011 2:56 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
After tweeking around with the code for a bit, I finally got it to work. Instead of using if else statements in the macro, i used a dobule trinary operator.

Here is the macro and some variations of it if you like

#ifndef SIGN_WORD
   #define SIGN_WORD(v)  ((v) == 0)? "ZERO" : ((v) < 0)? "NEGATIVE" : "POSITVE"
#endif

#ifndef SIGN_VALUE
   #define SIGN_VALUE(v) ((v) == 0 )? 0 : ((v) < 0)? -1 : 1
#endif


The first version returns the string word for the values classification, and the second one simply returns 1 if positive, -1 if negative or 0. This would be a great tool to pass in a value, and quickly get its sign, or even use it negate other values.

It would still be nice to see some feedback, on how to do this using the if else statements as well as comments on this macro.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron