Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

User0015 posted:

Bar.h - added "friend class Foo". It compiled. Doesn't work, but it compiled. That's always a good start.

What would you expect it to do other than compile? Your main function doesn't do anything. (Also I am very surprised that compiles.)

Adbot
ADBOT LOVES YOU

User0015
Nov 24, 2007

Please don't talk about your sexuality unless it serves the ~narrative~!

Avenging Dentist posted:

What would you expect it to do other than compile? Your main function doesn't do anything. (Also I am very surprised that compiles.)

Execute Barworks() and print text to the screen? (I added some lines to main to call Foowork)

Also, me too. If you have some better ways to do this, I'm all ears.

edit - It works. Somehow.

User0015 fucked around with this message at 23:58 on Aug 11, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I am hoping you made Barworks public, or you have a very strange compiler.

User0015
Nov 24, 2007

Please don't talk about your sexuality unless it serves the ~narrative~!
That's a good question. Lets see...

It still works with Barwork private. I lied. Forgot to comment a line. If you don't include private/public/protected, do classes default to private?

User0015 fucked around with this message at 00:17 on Aug 12, 2009

Vanadium
Jan 8, 2005

Yes. Structs default to public.

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Hello, I need more help. :(

I am writing a C++ program in VS 2008 that runs a piece of hardware so it has its own libraries and other stuff that it accesses. I am also using Boost.asio for this program. It's based on two separate programs that both run in VS just fine. When I try to compile, I get these errors:

code:
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(277) : error C2632: 'short' followed by 'short' is illegal
1>c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(277) : warning C4091: 'typedef ' : ignored on left of 'short' when no variable is declared
I'm guessing this is some weird compatibility issue because I of course did not write winnt.h and haven't seen anything like this before. What sorts of things would remedy this?

Lexical Unit
Sep 16, 2003

You may have seen this terrible horrible code. Help me learn how to make it better (more horrible).
code:
namespace detail
{
	namespace mpl = boost::mpl;

	template<char> struct numeral_c : mpl::integral_c<unsigned long, 0> { }; // bad?
	template<> struct numeral_c<'M'> : mpl::integral_c<unsigned long, 1000> { };
	template<> struct numeral_c<'D'> : mpl::integral_c<unsigned long, 500> { };
	template<> struct numeral_c<'C'> : mpl::integral_c<unsigned long, 100> { };
	template<> struct numeral_c<'L'> : mpl::integral_c<unsigned long, 50> { };
	template<> struct numeral_c<'X'> : mpl::integral_c<unsigned long, 10> { };
	template<> struct numeral_c<'V'> : mpl::integral_c<unsigned long, 5> { };
	template<> struct numeral_c<'I'> : mpl::integral_c<unsigned long, 1> { };
	template<> struct numeral_c<'i'> : mpl::integral_c<unsigned long, 1000> { };
	template<> struct numeral_c<'v'> : mpl::integral_c<unsigned long, 5000> { };
	template<> struct numeral_c<'x'> : mpl::integral_c<unsigned long, 10000> { };
	template<> struct numeral_c<'l'> : mpl::integral_c<unsigned long, 50000> { };
	template<> struct numeral_c<'c'> : mpl::integral_c<unsigned long, 100000> { };
	template<> struct numeral_c<'d'> : mpl::integral_c<unsigned long, 500000> { };
	template<> struct numeral_c<'m'> : mpl::integral_c<unsigned long, 1000000> { };

	template<class Total, class Iter>
	class numeral_fop
	{
		typedef typename mpl::deref<Iter>::type i;
		typedef typename mpl::deref<typename mpl::next<Iter>::type>::type next_i;
		typedef typename numeral_c<i::value>::type value;
		typedef typename numeral_c<next_i::value>::type next_value;
		
		public:
			typedef typename mpl::if_<
				mpl::less<value, next_value>
				, mpl::minus<Total, value>
				, mpl::plus<Total, value>
			>::type type;
	};
}

template<class String>
struct numeral :
	boost::mpl::iter_fold<
		String
		, boost::mpl::integral_c<unsigned long, 0>
		, detail::numeral_fop<boost::mpl::_1, boost::mpl::_2>
	>::type
{ };
Obviously I've cleaned it up a bit and removed as much stupid as I could find (I'm sure I haven't excised all of it yet). Notice the line near the top with the comment, "bad?" Initially I had wanted to make it so numeral_c<char>::type was only valid if the provided char was explicitly allowed.
code:
template<char> struct numeral_c { }; // default
But I kept getting the error, error: no type named ‘type’ in ‘struct detail::numeral_c<'\000'>’.

The reason is because in numeral_fop I advance the iterator to next and dereference to get the next character in the sequence. I use this information to determine if I should add the current value to the total, or subtract it from the total. However when the iterator is at the end of the sequence, I am advancing past the end of the sequence and dereferencing. This seems like a bad thing. I have the feeling like the code only coincidentally works.

I looked over the Boost.MPL documentation a bit and it seems like I could be using eval_if maybe somehow to avoid dereferencing an iterator past the end of the sequence. But I've tried and I can't seem to figure out how. I get the same error. Then I thought maybe I could use enable_if somehow. I'd have two versions of numeral_fop, one for when the iterator isn't at the end and another when it is. But again my skills were lacking and I just couldn't figure out how to get it to work like I wanted.

The best solution I could imagine would be to somehow iter_fold over [begin, end), but iter_fold doesn't seem to be configurable like that. It iterates over [begin, end] and that's that.

Can any of the boost users here come up with something to solve this?

Lexical Unit fucked around with this message at 00:24 on Aug 13, 2009

The Red Baron
Jan 10, 2005

This is hardly elegant, but it seems to work. iter_fold already iterates over [begin, end), so one solution is to just delegate the iterator dereferencing to another metafunction that isn't instantiated unless it can operate on a valid next-iterator.

code:
namespace mpl = boost::mpl;

namespace detail
{
  template<char> struct numeral_c;
  template<> struct numeral_c<'M'> : mpl::integral_c<unsigned long, 1000> { };
  template<> struct numeral_c<'D'> : mpl::integral_c<unsigned long, 500> { };
  template<> struct numeral_c<'C'> : mpl::integral_c<unsigned long, 100> { };
  template<> struct numeral_c<'L'> : mpl::integral_c<unsigned long, 50> { };
  template<> struct numeral_c<'X'> : mpl::integral_c<unsigned long, 10> { };
  template<> struct numeral_c<'V'> : mpl::integral_c<unsigned long, 5> { };
  template<> struct numeral_c<'I'> : mpl::integral_c<unsigned long, 1> { };
  template<> struct numeral_c<'i'> : mpl::integral_c<unsigned long, 1000> { };
  template<> struct numeral_c<'v'> : mpl::integral_c<unsigned long, 5000> { };
  template<> struct numeral_c<'x'> : mpl::integral_c<unsigned long, 10000> { };
  template<> struct numeral_c<'l'> : mpl::integral_c<unsigned long, 50000> { };
  template<> struct numeral_c<'c'> : mpl::integral_c<unsigned long, 100000> { };
  template<> struct numeral_c<'d'> : mpl::integral_c<unsigned long, 500000> { };
  template<> struct numeral_c<'m'> : mpl::integral_c<unsigned long, 1000000> { };

  template <typename Iter, typename End>
  struct lazy_next_value
  {
    typedef numeral_c<
      mpl::deref<Iter>::type::value
    > type;
  };

  template <typename Iter>
  struct lazy_next_value<Iter, Iter>
  {
    typedef mpl::integral_c<unsigned long, 0> type;
  };

  template<class Total, class Iter, class End>
  class numeral_fop
  {
    typedef typename numeral_c<mpl::deref<Iter>::type::value>::type value;
    typedef typename lazy_next_value<
        typename mpl::if_<
            boost::is_same<Iter, End>
          , mpl::always<Iter>
          , mpl::next<Iter>
        >::type::type
      , End
    >::type next_value;
		
    public:
      typedef typename mpl::if_<
          mpl::less<value, next_value>
        , mpl::minus<Total, value>
        , mpl::plus<Total, value>
      >::type::type type;
  };
}

template<class String>
struct numeral
  : mpl::iter_fold<
        String
      , mpl::integral_c<unsigned long, 0>
      , detail::numeral_fop<mpl::_1, mpl::_2, typename mpl::end<String>::type>
    >::type
{};
Also, in before TTS or AD post a 10x more elegant solution :shobon:.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

HondaCivet posted:

I'm guessing this is some weird compatibility issue because I of course did not write winnt.h and haven't seen anything like this before. What sorts of things would remedy this?

Chances are, somewhere there's a line like this before you include winnt.h:
code:
#define SHORT short
meaning that this happens:
code:
// winnt.h line 277:
typedef short SHORT;

// becomes...
typedef short short;
If you want to diagnose this, the easiest way in VS is to type SHORT somewhere and then right-click it and select "Go to definition". The solution is to #undef SHORT immediately before including winnt.h.

EDIT: Also, if you do find out who did #define SHORT short, you should yell at them.

Avenging Dentist fucked around with this message at 02:51 on Aug 13, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Oh, also regarding Lexical Unit's thing. I don't know why people get so into compile-time string parsing when no compiler supports the suffix operator yet. It always ends up being clumsy (in a bad way) and the suffix operator is going to eliminate a lot of the tedium of writing the entry point.

Lexical Unit
Sep 16, 2003

The Red Baron posted:

Also, in before TTS or AD post a 10x more elegant solution :shobon:.
Since you posted your compile-time parser, I was hoping you'd write up a response. I had gotten as far as passing in the end of the sequence as an additional template parameter, but I couldn't figure out how to leverage it properly. I kept trying to get the lazy-evaluation to work but it wasn't coming to me. Thanks :)

Avenging Dentist posted:

I don't know why people get so into compile-time string parsing when no compiler supports the suffix operator yet.
For me it certainly wasn't about the utility of what I wrote, but a way for me to explore Boost.MPL since at this time I don't really have any reason to otherwise; I mean in terms of the work I do. The suffix operator does look pretty cool, let's hope it doesn't go the way of Concepts!

Fecotourist
Nov 1, 2008

Avenging Dentist posted:

Oh, also regarding Lexical Unit's thing. I don't know why people get so into compile-time string parsing when no compiler supports the suffix operator yet. It always ends up being clumsy (in a bad way) and the suffix operator is going to eliminate a lot of the tedium of writing the entry point.

s:concepts:suffix operator:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Fecotourist posted:

s:concepts:suffix operator:

Hm yase a less-than-one page subsection is basically the same as a 22-page section, you got me there!

floWenoL
Oct 23, 2002

Fecotourist posted:

s:concepts:suffix operator:

If you're going to make a nerdy substitution joke the least you could do is get the order right.

Fecotourist
Nov 1, 2008

floWenoL posted:

If you're going to make a nerdy substitution joke the least you could do is get the order right.

Way to get it!
His post read as if it were written by AD passed through sed -e 's:concepts:suffix operator:g'. I.e. a variation on the standard (hah!) fetish. I just hope all this wonder ends up being C++1x and not 2x.

Edit: Malformed sed command!

Fecotourist fucked around with this message at 16:56 on Aug 13, 2009

Contero
Mar 28, 2004

Can someone (*cough*) show me how to build up an AST (my own data structures) using spirit? I've only ever used ANTLR as a parser generator which lets you return whatever you want as a result of parse rules, but spirit doesn't exactly do the same thing.

So how about this grammar:
code:
addExp = multExp ('+' multExp)*;
multExp = value ('*' value)*;
value = INT | '(' addExp ')';
Which would take things like: 5+3, 2+3*4 and (2+3)*(4+5*(2+8))

Using this AST:

code:
struct AST {
   virtual int eval() =0;
};

struct Int : public AST {
   Int(int val) : val(val) {}
   virtual int eval() {return val;}
   int val;
};

struct Add : public AST {
   Add(AST *left, AST *right) : left(left), right(right) {}
   virtual int eval() {return left->eval() + right->eval();}
   AST *left, *right;
};

struct Mult : public AST {
   Mult(AST *left, AST *right) : left(left), right(right) {}
   virtual int eval() {return left->eval() * right->eval();}
   AST *left, *right;
};
My idea for how to do this was to make a new class, Parser, which has a member function for each parse rule. I'd use boost::bind to pass function objects that call the member functions and keep track of the parsing progress inside the class. The problem is I'm not sure what the order spirit runs actions is, or what the most elegant solution to this is.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I really don't think you're doing that right at all. You should probably be looking at the documentation since the AST example is exactly what you're trying to do: http://www.boost.org/doc/libs/1_39_0/libs/spirit/classic/doc/trees.html

Contero
Mar 28, 2004

I guess I wanted to see if there was a way to use spirit without having to traverse their tree. :effort:

Edit: Went through the example, and I successfully modified it to spit out my AST. I'll start moving on to bigger and better parsers now. Thanks.

Contero fucked around with this message at 22:10 on Aug 13, 2009

The Red Baron
Jan 10, 2005

For your own sake, please look into Spirit2.1 instead. The attribute-based parsing is leaps and bounds better to work with than the old AST construction (which was apparently kinda hacked on in the first place)

6174
Dec 4, 2004
I just spent several hours documenting some old binary files. I've got two variants of an old C program that should read them (old as in written in K&R C, last updated in 1992). Neither work because they assume the width of ints, shorts and so forth that are not valid with my machine. I am updating the program to C99 so I can use the fixed-width types from stdint.h to avoid this issue, but that only works for integral types.

The problem is I've also got floats and doubles in this binary file (in IEEE format). I can't seem to find an equivalent to fixed-width types for floating points types or even a way to specify IEEE format. Since I don't want to have to mess with this program ever again, is there some way that I can read the data and put it into whatever underlying floating point format the particular compiler is using that has a reasonable chance of just working in the future with at most a recompile? Endianness shouldn't be a problem since I can detect it based on this particular file format (Except for some pathological counter examples but realistically those won't occur).

6174 fucked around with this message at 07:05 on Aug 14, 2009

Contero
Mar 28, 2004

I was kind of excited to try spirit 2.1 out, but after messing around with it for two hours I honestly find it to be much more cryptic and less easy to use than classic spirit. It also takes forever to compile.

Also walking the classic tree manually lets me use whatever code I want to construct a return value. I don't have to stuff it all into bits of lazy evaluated code sprinkled throughout the grammar.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Contero posted:

It also takes forever to compile.

Better than taking forever to run. Spirit Classic is unbelievably slow on some stuff.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

6174 posted:

The problem is I've also got floats and doubles in this binary file (in IEEE format). I can't seem to find an equivalent to fixed-width types for floating points types or even a way to specify IEEE format.

That's because the underlying format for floating point types is not defined by the ISO standard.

6174
Dec 4, 2004

Avenging Dentist posted:

That's because the underlying format for floating point types is not defined by the ISO standard.

I realize that. It also doesn't specify the underlying format for the five standard signed integer types, but there are also the intN_T / uintN_t types where it is specified. My problem is I can't find something corresponding to intN_t for floating point values (which may be because they don't exist).

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

6174 posted:

I realize that. It also doesn't specify the underlying format for the five standard signed integer types, but there are also the intN_T / uintN_t types where it is specified. My problem is I can't find something corresponding to intN_t for floating point values (which may be because they don't exist).

code:
#ifndef __STDC_IEC_559__
#error "Stop using a weird system"
#endif

/* If we get here...
   "float" means IEEE 60559 single format,
   "double" means IEEE 60559 double,
   "long double" means some (any) IEEE 60559 extended format (or double) */
See also Annex F of ISO/IEC 9899:1999.

Also keep in mind that intN_t and uintN_t are not mandated by the standard.

Avenging Dentist fucked around with this message at 08:18 on Aug 14, 2009

6174
Dec 4, 2004

Avenging Dentist posted:

See also Annex F of ISO/IEC 9899:1999.

That is what I get for not reading the Annexes. This should be plenty good enough for what I need. Though crazily enough there is yet another variant of this same program that does convert the IEEE values in the file to Cray floating point.

Avenging Dentist posted:

Also keep in mind that intN_t and uintN_t are not mandated by the standard.

The main thing I'd like is for it to just work with at most a recompile, but if that can't occur failing in a way that is obvious that the build environment is crazy should be good enough.

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Avenging Dentist posted:

Chances are, somewhere there's a line like this before you include winnt.h:
code:
#define SHORT short
meaning that this happens:
code:
// winnt.h line 277:
typedef short SHORT;

// becomes...
typedef short short;
If you want to diagnose this, the easiest way in VS is to type SHORT somewhere and then right-click it and select "Go to definition". The solution is to #undef SHORT immediately before including winnt.h.

EDIT: Also, if you do find out who did #define SHORT short, you should yell at them.

A few problems with this unfortunately . . .

1. For some reason the "go to definition" thing is greyed out whenever I try it on something, not even just shorts.
2. I searched the whole project and "#define SHORT short" never comes up. Unsigned short stuff comes up but that wouldn't count right?

halp :(

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

HondaCivet posted:

1. For some reason the "go to definition" thing is greyed out whenever I try it on something, not even just shorts.

Intellisense sucks sometimes. It can help if you delete the .ncb file from the project dir and then rebuild the project.

HondaCivet posted:

2. I searched the whole project and "#define SHORT short" never comes up. Unsigned short stuff comes up but that wouldn't count right?

The easy solution is
code:
#undef SHORT
#include <winnt.h>
#define SHORT short // Probably not necessary, but it might be

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Avenging Dentist posted:

Intellisense sucks sometimes. It can help if you delete the .ncb file from the project dir and then rebuild the project.


The easy solution is
code:
#undef SHORT
#include <winnt.h>
#define SHORT short // Probably not necessary, but it might be

Where should that code go? I tried in main, above where the other #includes are, in a header . . . it either doesn't do anything or makes a billion other errors pop up instead.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

HondaCivet posted:

Where should that code go? I tried in main, above where the other #includes are, in a header . . . it either doesn't do anything or makes a billion other errors pop up instead.

What are some of the other errors?

You could also try putting #include <windows.h> before any other #includes. You might want to put #define NOMINMAX before that since Windows is retarded.

ctz
Feb 6, 2003

Avenging Dentist posted:

Also keep in mind that intN_t and uintN_t are not mandated by the standard.

They are if you have integral types which can fit the requirements, which most implementations will.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

ctz posted:

They are if you have integral types which can fit the requirements, which most implementations will.

Yes congratulations you win the prize.

jonus
Jun 7, 2008

by mons all madden
So I'm messing around with Irrlicht, using code::blocks and compiling with the MS Visual C++ toolkit. I have iostream.h and fstream.h, but for the life of me I can't get it to let me open, read or write to a text file.




ofstream fout("name.txt");
fout << yourname;

fout << flush;
fout.close();

This kind of stuff just doesn't work. Last time I used Irrlicht I used the GCC compiler, and originally I cut my C++ teeth with borland, so doing anything in MS visual C++ is kinda confusing to me.

Also can someone explain to me why I can't just use cout, but instead have to use std::cout?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Someone hasn't used c++ since the standard got made:laugh:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
MSVC still has iostream.h??? :pwn:

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Avenging Dentist posted:

MSVC still has iostream.h??? :pwn:

So does gcc, but at least the latter is kind enough to inform you you're using a deprecated header

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum

jonus posted:

So I'm messing around with Irrlicht, using code::blocks and compiling with the MS Visual C++ toolkit. I have iostream.h and fstream.h, but for the life of me I can't get it to let me open, read or write to a text file.




ofstream fout("name.txt");
fout << yourname;

fout << flush;
fout.close();

This kind of stuff just doesn't work. Last time I used Irrlicht I used the GCC compiler, and originally I cut my C++ teeth with borland, so doing anything in MS visual C++ is kinda confusing to me.

Also can someone explain to me why I can't just use cout, but instead have to use std::cout?
Do I guess to explain a little more

std:: is a namespace, you can add using namespace std; at the top and you won't need the std:: in front of every cout. (pretty sure)

also, in VS you should use #include <iostream> and #include <fstream> with no .h
the ones with .h are old bad headers that suck, same games for most headers I think... like string.h is just string etc

pseudorandom name
May 6, 2007

Otto Skorzeny posted:

So does gcc, but at least the latter is kind enough to inform you you're using a deprecated header

The got rid of them over a year ago for 4.3.0's release.

ehnus
Apr 16, 2003

Now you're thinking with portals!

Avenging Dentist posted:

MSVC still has iostream.h??? :pwn:

I know 2008 does not, and I think it's been thrown away in 2005 as well.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Sweeper posted:

like string.h is just string etc

Hahahahahahahahaha no. These two headers have nothing to do with one another. (string.h is cstring in C++)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply