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
raminasi
Jan 25, 2005

a last drink with no ice
code:
polynomial & polynomial::operator = (const polynomial & other) {
is the syntax you want

Adbot
ADBOT LOVES YOU

Chuu
Sep 11, 2004

Grimey Drawer
I am trying to define some global error handling functions, with declaration and definition in different files. Visual Studio is fine with this code, but gcc gives me warning "warning: 'function name' declared 'static' but never defined." Can someone tell me exactly what I'm doing wrong?

code:
Error.h:

namespace common{
namespace Errors{

   static int Check(...);

}
}

Error.cpp:

   int common::Errors::Check(...)
   {
      definition here;
   }

ehnus
Apr 16, 2003

Now you're thinking with portals!

Chuu posted:

I am trying to define some global error handling functions, with declaration and definition in different files. Visual Studio is fine with this code, but gcc gives me warning "warning: 'function name' declared 'static' but never defined." Can someone tell me exactly what I'm doing wrong?

Static functions don't have visibility outside of their compile units, meaning the linker doesn't (or at least shouldn't) see static functions that aren't defined in the compile unit. In other words, if you want to use a function in one .cpp file that is defined in another then don't mark it static.

Contero
Mar 28, 2004

What flags do I pass to gcc to get it to compile sparc (:() assembly into a sparc binary on an intel machine?

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

Contero posted:

What flags do I pass to gcc to get it to compile sparc (:() assembly into a sparc binary on an intel machine?

Stock GCC can't cross-compile like this, you need to build GCC specially as a cross compiler.

Chuu
Sep 11, 2004

Grimey Drawer

ehnus posted:

Static functions don't have visibility outside of their compile units, meaning the linker doesn't (or at least shouldn't) see static functions that aren't defined in the compile unit. In other words, if you want to use a function in one .cpp file that is defined in another then don't mark it static.

Ugh, ok. The reason I am trying to do it like this is that this error handling code pretty much is included in every single file in the project, and the error handling code itself reaches tendrils into said code quite a bit. I need the "wall" between declaration and implementation to prevent an absolute ton of circular dependencies.

I don't really have the freedom to change the code that links to the error function. Is there any way around this problem if I wanted to keep it as static functions without completely forward declaring entire class interfaces? For other reasons I really don't want to change the design to something like a singleton to solve this problem.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Chuu posted:

Ugh, ok. The reason I am trying to do it like this is that this error handling code pretty much is included in every single file in the project, and the error handling code itself reaches tendrils into said code quite a bit. I need the "wall" between declaration and implementation to prevent an absolute ton of circular dependencies.

I don't really have the freedom to change the code that links to the error function. Is there any way around this problem if I wanted to keep it as static functions without completely forward declaring entire class interfaces? For other reasons I really don't want to change the design to something like a singleton to solve this problem.

I don't understand. Why can't you just get rid of the static keyword and make it a global function? You don't have to make it a method of some class.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Chuu, the key point that you're missing here is that, for source compatibility with C, static means something different on members and non-members. On members, it means (roughly) "this belongs to the class, not to instances of the class"; on non-members, it means "this belongs to this translation unit and cannot be accessed from a different module". Everything declared outside of a class is "static" in the sense of not being attached to an instance of some class. Yes, this is needlessly confusing; blame C.

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

rjmccall posted:

Chuu, the key point that you're missing here is that, for source compatibility with C, static means something different on members and non-members. On members, it means (roughly) "this belongs to the class, not to instances of the class"; on non-members, it means "this belongs to this translation unit and cannot be accessed from a different module". Everything declared outside of a class is "static" in the sense of not being attached to an instance of some class. Yes, this is needlessly confusing; blame C.

Actually blame Bjarne Stroustroup for reusing a keyword in a rather unintuitive fashion

Chuu
Sep 11, 2004

Grimey Drawer
Ok, thanks for the clarification. Coming from Java the fifty billion uses of 'static' is incredibly confusing.

ok go etc
Sep 15, 2005

bach, kafka and licks
I have little to no idea of what I'm doing here so please forgive me if the answer is obvious. I tried googling around but to no avail.

I have this C/C++ source for macos x that compiles to a static library just fine. I need that library to be a shared library. What do I do ? Is it "easy enough" and only involves tweaking the makefile or does it require extensive modification of the source ?

For those interested in a bit more background, libtcod is a library for roguelike development in C/C++. The wrapper for Python is based on ctypes which requires shared libraries, but only the static ones are available for mac. (I am not the developer, in case you were wondering).

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.

Chuu posted:

Ok, thanks for the clarification. Coming from Java the fifty billion uses of 'static' is incredibly confusing.
I don't know why you'd assume that it works the same way as in Java and then blame C++ (instead of yourself) when you used it in a circumstance that doesn't exist in Java.

Chuu
Sep 11, 2004

Grimey Drawer

Mustach posted:

I don't know why you'd assume that it works the same way as in Java and then blame C++ (instead of yourself) when you used it in a circumstance that doesn't exist in Java.

I don't see anywhere in that quote I'm blaming C++ :confused:

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.
Maybe I jumped to conclusions, but when people say "coming from X, the fifty billion uses of Y in Z is incredibly confusing," — when there's actually just three uses for Y — it comes across as blaming Z for their assumption being wrong.

Mustach fucked around with this message at 18:04 on Mar 2, 2009

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!
Why does the following code compile in C++ mode, but not in C?

code:
while( 1 )
{
foo:
unsigned int n = bar();
//...
}
Moving the declaration of n outside of the loop fixes it, as does removing the label. This is in GCC 4.1.something. I can't quite remember the error, it was "expected ??? before unsigned"

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
C89 demands that declarations be the first thing in a block.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Avenging Dentist posted:

C89 demands that declarations be the first thing in a block.

Yeah, but GCC doesn't seem to care about that in general.

Hung Yuri
Aug 29, 2007

by Tiny Fistpump
I'm having a hard time figuring out where I should go with this assignment. I'm supposed to simulate a vending machine where there are a bunch of things worth a certain amount, and ultimately gives them the item and returns whatever cash they have left in the smallest possible amount of coins. For example, 35 cents remainder would return a quarter and a dime. If you don't have enough money to buy the item, it should say deposit more money or select a different product (I'm thinking here I should throw the user a choice, choosing to deposit money brings him a scanf where it'll add to the amount of money he put in, where the other choice he sees the menu again.)

I'm just trying to figure out how I'd go about this. I was thinking a dowhile loop where it would print the menu, the user makes a selection and an action happens and after that my brain just goes :saddowns:

I've got this so far but now I'm thinking I shouldn't use a do while for this

http://pastebin.com/m638b3b65

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
A do {} while () loop is just fine here. Loop structure should not be the major problem in this assignment. Talk to your TA if you're really blocking on this.

Also, float cannot store hundredths with perfect precision; you should strongly consider representing currency using whole numbers of cents rather than fractional numbers of dollars.

Hung Yuri
Aug 29, 2007

by Tiny Fistpump

rjmccall posted:

A do {} while () loop is just fine here. Loop structure should not be the major problem in this assignment. Talk to your TA if you're really blocking on this.

Also, float cannot store hundredths with perfect precision; you should strongly consider representing currency using whole numbers of cents rather than fractional numbers of dollars.

I'm not sure how else to discern between what is a dollar or a penny if say the user puts in "1"

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Gvaz posted:

I'm not sure how else to discern between what is a dollar or a penny if say the user puts in "1"

You should definitely assume that the user is giving you dollar amounts (unless the assignment tells you otherwise); I'm just saying that internally, you will save yourself much grief if you represent (say) $3 as the integer 300 instead of the floating-point value 3.0.

EDIT: and similarly, you would also print the numbers as dollar amounts; I'm just talking about the internal types that your program uses to store numbers.

rjmccall fucked around with this message at 23:33 on Mar 2, 2009

Hung Yuri
Aug 29, 2007

by Tiny Fistpump

rjmccall posted:

You should definitely assume that the user is giving you dollar amounts (unless the assignment tells you otherwise); I'm just saying that internally, you will save yourself much grief if you represent (say) $3 as the integer 300 instead of the floating-point value 3.0.

It just says "must be capable of accepting any coins as well as dollar bills" but I'll try doing the numbers like that, inside the program.

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

Gvaz posted:

I'm not sure how else to discern between what is a dollar or a penny if say the user puts in "1"

scanf() formats can help you here, something like "$%d.%d" could be useful.

Hung Yuri
Aug 29, 2007

by Tiny Fistpump

Otto Skorzeny posted:

scanf() formats can help you here, something like "$%d.%d" could be useful.

I'm not sure I can do that because it says "must be returned with the smallest possible amount of coins" instead of a straight value.

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

Gvaz posted:

I'm not sure I can do that because it says "must be returned with the smallest possible amount of coins" instead of a straight value.

I'm talking about taking input re: how much money the user is 'paying'. Your algo for returning coins is unrelated.

hey mom its 420
May 12, 2007

Gonna be working on some pretty complex stuff in C in the near future, thinking about doing some unit test coverage, what's the best unit testing framework for C?

Red Blaze
Dec 9, 2008

Not very red.
Is there some way to cause a previously defined function to give a compiler error when used? I would like to cause the OpenGL function glColor4f() to give an error, so I remember to use my own.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Red Blaze posted:

Is there some way to cause a previously defined function to give a compiler error when used? I would like to cause the OpenGL function glColor4f() to give an error, so I remember to use my own.

You could define glColor4f as a macro that produces uncompilable garbage. I'm not sure if GCC lets you define parameterized macros as precompiler parameters but that would probably be the cleanest way to do it.
Edit: -D'glColor4f(params)=DO_NOT_USE_THIS_FUNCTION=0;/*params*/' or whatever should work

Painless fucked around with this message at 02:29 on Mar 3, 2009

Red Blaze
Dec 9, 2008

Not very red.

Painless posted:

You could define glColor4f as a macro that produces uncompilable garbage. I'm not sure if GCC lets you define parameterized macros as precompiler parameters but that would probably be the cleanest way to do it.

This works great. I will just use this macro so I know the context of the error.

#define glColor4f(r,g,b,a) glColor4fDontUse(r*g*b*a)

TheSpook
Aug 21, 2007
Spooky!
Visual Studio question:

I am setting compile options for a single source file ("prime.cu" in this case).

code:
"$(CUDA_BIN_PATH)\nvcc.exe" -ccbin "$(VCInstallDir)bin" -c -D_DEBUG -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler /EHsc,/W3,/nologo,/Wp64,/Od,/Zi,/MTd -I"$(CUDA_INC_PATH)" -I./ -I"$(NVSDKCUDA_ROOT)\common\inc" -o $(ConfigurationName)\prime.obj prime.cu
Is there a $(VARIABLE) that stands for the name of the file I am compiling ("prime.cu") . . .?

TheSpook
Aug 21, 2007
Spooky!

Red Blaze posted:

This works great. I will just use this macro so I know the context of the error.

#define glColor4f(r,g,b,a) glColor4fDontUse(r*g*b*a)

Don't know the context of this, but something like

#define glColor4f(r,g,b,a) glColor4fDontUse((r)*(g)*(b)*(a))

will be safer.

Edit: Actually read the context, it would appear that you are not going for correctness ;).

TheSpook fucked around with this message at 04:48 on Mar 3, 2009

POKEMAN SAM
Jul 8, 2004

Red Blaze posted:

This works great. I will just use this macro so I know the context of the error.

#define glColor4f(r,g,b,a) glColor4fDontUse(r*g*b*a)

Can't you just undefine it?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Why are you even worrying about parameters?
code:
#define glColor4f(r,g,b,a) glColor4fDontUse()

That Turkey Story
Mar 30, 2003

Ugg boots posted:

Can't you just undefine it?

No.

Chuu
Sep 11, 2004

Grimey Drawer
Does anyone know if there is a free library that offers thread-local storage without the requirement that the thread was created in a particular way?

I was looking at ACE's implementation of Thread-Local storage but it carries the requirement that the thread inherit from ACE_Task_Base.

Chuu
Sep 11, 2004

Grimey Drawer

Bonus posted:

Gonna be working on some pretty complex stuff in C in the near future, thinking about doing some unit test coverage, what's the best unit testing framework for C?

I don't know about best, but CPPUnit is very mature and has served my needs on a very complicated project. Bonus points for the fact it has an XML outputter which with xlst translation means I can use its output to feed any CI tool that was build around junit/nunit.

CPPUnit doesn't "plug into" your code like some Unit testing tools in other languages, you pretty much have a separate main and wrappers for all the classes you are testing.

That Turkey Story
Mar 30, 2003

Chuu posted:

Does anyone know if there is a free library that offers thread-local storage without the requirement that the thread was created in a particular way?

Boost.Thread

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

Chuu posted:

I don't know about best, but CPPUnit is very mature and has served my needs on a very complicated project. Bonus points for the fact it has an XML outputter which with xlst translation means I can use its output to feed any CI tool that was build around junit/nunit.

CPPUnit doesn't "plug into" your code like some Unit testing tools in other languages, you pretty much have a separate main and wrappers for all the classes you are testing.

"C"

Fullets
Feb 5, 2009

ok go etc posted:

I have this C/C++ source for macos x that compiles to a static library just fine. I need that library to be a shared library. What do I do ? Is it "easy enough" and only involves tweaking the makefile or does it require extensive modification of the source ?

I think what you want to do is compile your library as a shared library rather than converting your existing static one. A command line something like
code:
g++ object.o … -o libsomelib.dylib -install_name libsomelib.dylib -dynamiclib link-options …
worked for me in a similar situation. There are about a thousand other options you can add that the gcc/ld manpages detail.

I shouldn't think source modification would be required.

Adbot
ADBOT LOVES YOU

POKEMAN SAM
Jul 8, 2004

That Turkey Story posted:

No.

Oh, I was thinking the original one was a macro defined outside of his project so he was stuck with it. My bad.

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