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
vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
Oh i figured mine out. Turns out pipes are one way, and i was writing to the wrong end.

Adbot
ADBOT LOVES YOU

TSDK
Nov 24, 2003

I got a wooden uploading this one

Vanadium posted:

It says

code:
foo.cpp:16: error: cast from ‘Node*’ to ‘uint’ loses precision
foo.cpp:16: error: cast from ‘Node*’ to ‘uint’ loses precision
for me :shobon:
The clue's in the message then: don't cast to uint.

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha

TSDK posted:

The clue's in the message then: don't cast to uint.

Pointer and int are usually the same size though...

edit: even if they are different sizes, he's setting next to 0. If he were only
printing 4 out of the 8bytes used in the pointer, he still shouldn't be getting 64.
I'm blaming printf.

You could prove that your printf statement is to blame by doing an:
[code]
if(node->next == 0)
{
printf("I, printf, suck wang\n");
}

Since you're using g++, try std::cout.

vanjalolz fucked around with this message at 16:17 on May 1, 2008

Mr VacBob
Aug 27, 2003
Was yea ra chs hymmnos mea
One of your printf formats is being ignored (probably because it doesn't like "%Lfx") so it's reading the wrong thing off the stack.

And use %p instead of casting pointers to something else.

more falafel please
Feb 26, 2005

forums poster

vanjalolz posted:

Pointer and int are usually the same size though...

NEVER assume this. You have no idea how many times this comes up. If you want your code to compile on 64-bit, DO NOT ever assume this. Like, ever.

Ever.

Paniolo
Oct 9, 2007

Heads will roll.
I'm wondering if anything like this exists:

code:
struct Foo {
   int bar(int, float, std::string, DerivedClass*);
};

BaseClass& ref = instanceOfDerivedClass;
float x = call<float>(&Foo::bar, new Foo(), 1.0f, 20, "Hello world", ref);
Basically, a universal function adapter ("call" in the snippet above) that automatically and intelligently converts all parameters to the types expected by the wrapped function including stuff like dereferencing pointers/taking address of references, performing dynamic_casts of polymorphic types, detecting if its a free function pointer or pointer to member, etc., with either run-time or compile-time type checking.

I'm asking because I've implemented about 80% of it but I'd love to find out it's already in boost and I'm just terrible at finding it.

floWenoL
Oct 23, 2002

more falafel please posted:

NEVER assume this. You have no idea how many times this comes up. If you want your code to compile on 64-bit, DO NOT ever assume this. Like, ever.

Ever.

Or at least use uintptr_t :shobon:.

elevatordeadline
Jan 29, 2008

vanjalolz posted:

I don't like the look of that, you're defining a struct Node then you're typedef'ing over it. You're using g++ so you dont need the type def anyway, change it to
code:
struct Node { ldouble coeff; ulong power; Node* next; }
The reason for the typedef is because this was at one point C code, and I didn't want to have struct nodes all over the place.

vanjalolz posted:

Are you on a 64 bit system? I think the proper way to printf pointers is %p.
I'm not, but you're right.

I don't get it, though. The new codepad output is still correct, but mine is still nonsense.
code:
>: g++ -Wall -Wextra -pedantic -o a.exe a.cpp

>: a.exe
Node at 0x000324E8 says -0.000000x^16386 and points to 0x00000064.
Also:

MrVacBob posted:

One of your printf formats is being ignored (probably because it doesn't like "%Lfx") so it's reading the wrong thing off the stack.
I don't see why that would even be true. If I don't use %Lf, the compiler warns about a format-argument mismatch, and using either of "%Lfx" or "%Lf x" makes no difference.

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha

elevatordeadline posted:

The reason for the typedef is because this was at one point C code
Then you should do typedef struct Node_t { .. } Node;


elevatordeadline posted:

>: a.exe
Node at 0x000324E8 says -0.000000x^16386 and points to 0x00000064.[/code]
You're setting the pointer to 0, and yet it thinks the value held is 0x64. There is no situation where that is possible, the printf'ing must be wrong.

Vanadium
Jan 8, 2005

This totally would not have happened if you were actually using C++ properly

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

Andrei Alexandrescu posted:

Indeed, where there are ellipses, there's not much C++ left.

Paniolo
Oct 9, 2007

Heads will roll.

Andrei Alexandrescu posted:

Indeed, where there are ellipses, there's not much C++ left.

Unless it's C++0x.

TSDK
Nov 24, 2003

I got a wooden uploading this one

elevatordeadline posted:

I don't get it, though. The new codepad output is still correct, but mine is still nonsense.
I think your dev environment is misconfigured. By the looks of it, either there's a mismatch in the calling convention or the long double support between your executable and the library you're linking against.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

vanjalolz posted:

Then you should do typedef struct Node_t { .. } Node;
There's not really a "should" here. All of these work for bypassing the struct namespace:
code:
typedef struct Node { ... } Node;
code:
struct Node { ... };
typedef struct Node Node;
code:
typedef struct Node Node;
struct Node { ... };
code:
typedef struct { ... } Node;

haveblue
Aug 15, 2005



Toilet Rascal

Mustach posted:

There's not really a "should" here. All of these work for bypassing the struct namespace:
code:
typedef struct Node { ... } Node;
code:
struct Node { ... };
typedef struct Node Node;
code:
typedef struct Node Node;
struct Node { ... };
code:
typedef struct { ... } Node;

When using all except the last, make sure the struct's real name is different from the one created by the typedef.

Vanadium
Jan 8, 2005

HB posted:

When using all except the last, make sure the struct's real name is different from the one created by the typedef.

Why?

haveblue
Aug 15, 2005



Toilet Rascal

Vanadium posted:

Why?

To avoid a conflict, that's how I remember learning it v:)v

I just tried it and apparently I have been misled all these years.

Avenging Dentist
Oct 1, 2005

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

HB posted:

To avoid a conflict, that's how I remember learning it v:)v

I just tried it and apparently I have been misled all these years.

Just don't do the following in C++ (works in C):
code:
#include <stdio.h>

typedef struct foo
{
    char x;
} bar;

typedef struct bar
{
    int x;
} foo;

int main()
{
    printf("%d",sizeof(foo)); // Prints "4" in C
}

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha

HB posted:

When using all except the last, make sure the struct's real name is different from the one created by the typedef.

That's what I was getting at. You don't want to be at the mercy of the compiler with something like this. Be explicit and show that there is a difference between the struct and the typedef.

Vanadium
Jan 8, 2005

From the boostcon thread:

The Red Baron posted:

To keep this post slightly more on-topic, how long should we expect to have to wait before C++0x is incorporated in most major compilers, assuming that they actually manage to get the standard out next year? For GCC I'm sure it won't be long at all, but it'd suck if we had to wait an entire Visual Studio cycle before the vcpp compiler got upgraded

The gcc guys list a bunch of features they already implement at http://gcc.gnu.org/gcc-4.3/cxx0x_status.html, and Herb Sutter has alluded that MSVC already implements a bunch of stuff as well. To be honest, I expect MSVC to have a "complete" implementation sooner than gcc.

PIGEOTO
Sep 11, 2007

I've got a question about an error I've been getting and it's driving me nuts because I just can't figure out what is the problem. This is the error in MSVC++;

error C2143: syntax error : missing ';' before '*'

I've gotten these kinds of errors before and there has always been a logical way out of them, but this time I just can't see what the problem is so maybe you guys can help me out. Here's my code;

http://pastebin.com/m23d01ab6

If you want me to paste more files, then let me know.

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
You are including levelclass.h in your playerclass.h, so it is used before it is defined.

Either move line 40 to 52+, or completely remove it.

The ; before * message is usually part of a bigger message that says that a type can not be found.

edit: To make it clearer:

LevelClass uses PlayerClass, but PlayerClass.h includes LevelClass.h BEFORE it defines what PlayerClass is,
therefore the compiler trips up trying to figure out what playerclass is in levelclass.

vanjalolz fucked around with this message at 13:24 on May 3, 2008

PIGEOTO
Sep 11, 2007

vanjalolz posted:

You are including levelclass.h in your playerclass.h, so it is used before it is defined.

Either move line 40 to 52+, or completely remove it.

The ; before * message is usually part of a bigger message that says that a type can not be found.

edit: To make it clearer:

LevelClass uses PlayerClass, but PlayerClass.h includes LevelClass.h BEFORE it defines what PlayerClass is,
therefore the compiler trips up trying to figure out what playerclass is in levelclass.

drat, this has been getting me for the past few hours and that's all it was. Probably happened because I've been playing around with the player and level class to get the communication flow just right and accidently forgot to delete that #include. Thanks a lot!

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

vanjalolz posted:

Be explicit and show that there is a difference between the struct and the typedef.
A typedef name is just an alias — there isn't any difference if you're talking about types.

Struct tags occupy a different namespace than typedef names, so it's like what HB found out: there won't be any conflict if a typedef name is the same as a struct tag. With struct Blah { int x; };, the name of the struct isn't "Blah", it's "struct Blah".

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha

Mustach posted:

the name of the struct isn't "Blah", it's "struct Blah".

In C, yes. What about c++ (which he is using to compile)? Isn't Blah a synonym for struct Blah? Its also a synonym for a typedef...

edit: both of which mean the same thing! Ok I get it, but I still disagree. Its messy and confusing! the standard is typedef struct x_t { ... } x, *px; so stick with it :D

ehnus
Apr 16, 2003

Now you're thinking with portals!

vanjalolz posted:

edit: both of which mean the same thing! Ok I get it, but I still disagree. Its messy and confusing! the standard is typedef struct x_t { ... } x, *px; so stick with it :D

Whose standard is that?

tyrelhill
Jul 30, 2006

ehnus posted:

Whose standard is that?

Thats the way it is in most win32 headers.

ColdPie
Jun 9, 2006

ehnus posted:

Whose standard is that?

I've always seen structs typedef'd either like that (append _t), or with an underscore on either side of the struct name. I think it's just the defacto standard, like using i and j for index variables. No one's going to eviscerate you for not using it, but it's just what "usually" is used.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Well, I think people will probably eviscerate you if you do it in C++. :colbert:

6174
Dec 4, 2004
I've got a template question. I have two functions:
code:
template<class T>
vector<T> arbitrary_partition(T number, vector<T> partite_sizes);

template<class T>
T arbitrary_partition(T number, vector<T> partite_sizes);
The first function calculates the number of ways to split the number into partitions with partite sizes coming from the vector partite_sizes. The return is a vector of size number + 1 where at position i is the number of ways that i can be partitioned.

What I want is the second function to basically be
code:
template<class T>
T arbitrary_partition(T number, vector<T> partite_sizes)
{
     return arbitrary_partition(number, partite_sizes)[number];
}
(This function may be wrong as I haven't gotten it to compile)

However when I have both functions, and invoke the first as:
code:
const unsigned long num = 100;

vector<unsigned long> partite (num,0);

for (unsigned long i = 0; i <= num; ++i) {
      partite[i] = i+1;
}

vector<unsigned long> count = arbitrary_partition(num, partite);
I get the error:
code:
/home/6174/Projects/projecteuler/src/0071-0080/problem76.cpp: In static member function ‘static void Problem76::solve()’:
/home/6174/Projects/projecteuler/src/0071-0080/problem76.cpp:36: error: call of overloaded 
     ‘arbitrary_partition(const long unsigned int&, std::vector<long unsigned int, std::allocator<long unsigned int> >&)’ is ambiguous
/home/6174/Projects/projecteuler/src/0071-0080/../shared/partition.h:48: note: candidates are: std::vector<T, std::allocator<_CharT> >
     arbitrary_partition(T, std::vector<T, std::allocator<_CharT> >) [with T = long unsigned int]
/home/6174/Projects/projecteuler/src/0071-0080/../shared/partition.h:65: note:                 
     T arbitrary_partition(T, std::vector<T, std::allocator<_CharT> >) [with T = long unsigned int]
make[2]: *** [CMakeFiles/projecteuler.dir/src/0071-0080/problem76.o] Error 1
make[1]: *** [CMakeFiles/projecteuler.dir/all] Error 2
make: *** [all] Error 2
My problem is I don't see what is ambiguous. It would seem to me that since count is a vector, it should be obvious that the function to use is the one returning a vector. What am I doing wrong, or how can I make this non-ambiguous?

If it makes a difference this is with g++ (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

edit:

For reference:
code:
unsigned long count = arbitrary_partition(num, partite);
Fails in the same way, where I would have expected it to use the second function returning type T.

6174 fucked around with this message at 00:48 on May 5, 2008

ColdPie
Jun 9, 2006

6174 posted:

I've got a template question. I have two functions:
code:
template<class T>
vector<T> arbitrary_partition(T number, vector<T> partite_sizes);

template<class T>
T arbitrary_partition(T number, vector<T> partite_sizes);

Your trouble is right at the start. These two functions have the same signature: arbitrary_partition(T, vector<T>). There's no way for the compiler to distinguish between them. You'll either need to give the functions different names, or change the parameters they accept.

6174
Dec 4, 2004

ColdPie posted:

Your trouble is right at the start. These two functions have the same signature: arbitrary_partition(T, vector<T>). There's no way for the compiler to distinguish between them. You'll either need to give the functions different names, or change the parameters they accept.

That would explain it. I thought the return value was considered for deciding what to do with overloaded functions. I guess not, and 7.4.1 of Stroustrup explains why.

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
For a programming project, I need to write a real basic interpreter for a scheme like language. All it really needs to do is be able to handle basic primitives, if statements, literals, numbers, local variable bindings..and that's probably about it. I was hoping I could download the source code for something similar as a reference point, but so far I've been unable to download anything that it isn't wayyyy too complex..does anyone know where I might be able to download a basic c++ interpreter like this?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Soldat posted:

For a programming project, I need to write a real basic interpreter for a scheme like language. All it really needs to do is be able to handle basic primitives, if statements, literals, numbers, local variable bindings..and that's probably about it. I was hoping I could download the source code for something similar as a reference point, but so far I've been unable to download anything that it isn't wayyyy too complex..does anyone know where I might be able to download a basic c++ interpreter like this?

Are you constrained to a C/C++ host language? Because Scheme is really easy to write an interpreter for, but C and C++ are really not the greatest languages for the task, and any non-trivial interpreter in them is going to be at least fairly complex.

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect

ShoulderDaemon posted:

Are you constrained to a C/C++ host language? Because Scheme is really easy to write an interpreter for, but C and C++ are really not the greatest languages for the task, and any non-trivial interpreter in them is going to be at least fairly complex.

Unfortunately I am constrained to c++. The interpreter itself will be somewhat trivial I think, everything else I've found has been way too extensive. Any other suggestions?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Soldat posted:

Unfortunately I am constrained to c++. The interpreter itself will be somewhat trivial I think, everything else I've found has been way too extensive. Any other suggestions?

Well, TinyScheme is the C version I would suggest studying. The opcode evaluator uses some occasional magic because it's designed for embedded systems, but the garbage collector and handling of closures and continuations is about as straightforward as you're likely to see outside of a textbook. It's only about 5300 lines of code for a very large portion of the R5RS.

The only really tricky bits to reading it are that the author occasionally scatters his structure definitions around in the code; a program like cscope will probably help a lot. Oh, and it uses lots of conditional compilation, but if that bothers you just run it through a preprocessor step or manually remove the features you don't care about.

Paniolo
Oct 9, 2007

Heads will roll.
Here's a tutorial for writing a small Scheme interpreter in Scheme. I think it would be straightforward to take most of the concepts and re-implement them in C++.

For parsing, you could look into boost::spirit, which lets you specify grammars in EBNF-like syntax. Given the syntactical simplicity of Scheme you could probably get a parser up and running relatively easily.

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

I have a Data Structures final today (in about 4 hours) and I am trying to get the gist of what everything is about. One of the big problems I had this semester was trying to wrap my head around Recursion. I had this example question for a review:

Q: Write a recursive function for f(x) = f(x-1) + 4x + 3 where f(0) = 1.

Is this correct?

code:
int recursive(int x){
    if (x != 0)
        return (recursive(x-1) + 4(x) - 3);

    else
        return 1;
}
I am unsure to be honest and would like to confirm this before I walk into the classroom thinking of the wrong thing.

ColdPie
Jun 9, 2006

notMordecai posted:

I have a Data Structures final today (in about 4 hours) and I am trying to get the gist of what everything is about. One of the big problems I had this semester was trying to wrap my head around Recursion. I had this example question for a review:

Q: Write a recursive function for f(x) = f(x-1) + 4x + 3 where f(0) = 1.

Is this correct?

Just kind of at a first glance (about to run out the door to school myself), this looks all right. I'd add in a check to make sure x isn't negative -- that'd create an infinite loop. Depending on exactly how the requirements are worded, I'd throw the check for <=0 at the start:

code:
int recursive(int x){
  if( x <= 0 )
    return 1;
  else
    return (/*blah blah*/);
}
This will return 1 on an invalid (negative) number, though maybe the assignment calls for an exception to be thrown or something.

Anyways looks fine to me. Good luck.

Adbot
ADBOT LOVES YOU

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Recursion-wise that looks right - the only problem is "4(x)" won't compile, it's "4*x".

ColdPie posted:

Just kind of at a first glance (about to run out the door to school myself), this looks all right. I'd add in a check to make sure x isn't negative -- that'd create an infinite loop. Depending on exactly how the requirements are worded, I'd throw the check for <=0 at the start:

This is always a good idea, and falls under "always check for unexpected inputs". The function will never get down to a negative number while it's recursing, but it'll break if someone else calls "recurse(-1)". The easiest and best fix is to change it to "unsigned int recurse(unsigned int x)" so it's not even possible to pass it a negative number.

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