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
Jewdicator
Oct 22, 2006
Somehow I managed to screw up (and save) my sourceannotations.h file, and it's now giving me errors which are beyond my comprehension. Where can I get a fresh sourceannotations.h file?

Adbot
ADBOT LOVES YOU

Chuu
Sep 11, 2004

Grimey Drawer

That Turkey Story posted:

Boost.Thread

I swore I read somewhere this only works for threads launched by boost::thread, but I can't find any reference to this in the docs. I'll check it out, thanks.

MrMartyn
Mar 6, 2007

Not too sure if this is the right place to ask but I thought I'd start here before I created my own thread.

I'm trying to make a till system that puts the amount that the user inputs into an array but the numbers stored seem to be random and when displaying the total it always seems to be 2.3125 even if the user just entered 1. Do I need to format it or something? I have no idea what to do now. Sorry the codes pretty messy, I've been trying a number of things and I removed half of it so it doesn't take up a whole page.

code:
#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream>


using namespace std;

void Purchase();





int twopound,onepound,fiftypence,twentypence,tenpence,fivepence,twopence,onepence;
int twopoundstatus,onepoundstatus,fiftypencestatus,twentypencestatus,tenpencestatus,fivepencestatus,twopencestatus,onepencestatus;

int main()
{
    cout << "\n\n\n\n\n\n\n\n\n\n\t\t\tWelcome to Ruddles Department Store";
    getchar();
    
    
	int select = 4;
	do
	{
        system("CLS");//clear screen
        
		cout << "\t\t\t" << char(201) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205)<< char(205)<< char(205) << char(205) << char(205) << char(205) << char(205)<< char(205)<< char(205) << char(205) << char(187) << "\n";
        cout << "\t\t\t" << char(186) << "          Main Menu        " <<char(186) << "\n";
        cout << "\t\t\t" << char(204) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205)<< char(205)<< char(205)<< char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(185) << "\n";
        cout << "\t\t\t" << char(186) << "\t                    " << char(186) << "\n";
        cout << "\t\t\t" << char(186) << "\t" << "1. Purchase         " << char(186) << "\n";
		cout << "\t\t\t" << char(186) << "\t" << "2. View Float       " << char(186) << "\n";
		cout << "\t\t\t" << char(186) << "\t" << "3. Enter Float      " << char(186) << "\n";
		cout << "\t\t\t" << char(186) << "\t" << "4. View Purchases   " << char(186) << "\n";
		cout << "\t\t\t" << char(186) << " \t                    " << char(186) << "\n"; 
		cout << "\t\t\t" << char(200) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(188) << "\n";
		cout << "\nPlease make a selection: ";

		cin >> select;

		switch (select)
		{
			case 1 : Purchase(); break;
			default : cout << "***Error Input***";
		} //end switch/case


	}
	while ( select != 4 );
	cout <<"\n\nPress enter to complete shut down...";
	getchar();
	getchar();

}

void Purchase()
{
     char Contin;
     int Sale;
     float Total;
     double AmountGiven;
     double ChangeDue;
     float PurchaseArray[6] = { 0, 0, 0, 0, 0, 0 };
     
     
     twopoundstatus = onepoundstatus = fiftypencestatus = twentypencestatus = tenpencestatus = fivepencestatus = twopencestatus = onepencestatus = 0;
     do
     { 
       cout << PurchaseArray[0] << "\n";
       cout << PurchaseArray[1] << "\n";
       cout << PurchaseArray[2] << "\n";
       cout << PurchaseArray[3] << "\n";
       cout << PurchaseArray[4] << "\n";
       cout << PurchaseArray[5] << "\n";
       
         
       cout << "\nPlease enter the amount of the sale: " << char(156);
       cin >> Sale;
       
      
       
       Total = Total + Sale;
       
       cout << "\nWould you like to enter another item? ";
       cin >> Contin;
     } 
     
     while ( Contin == 'y');
     
     for (int i = 0; i < 10; i++)
     {
         PurchaseArray[i]=Sale;
     }
     
     cout << "\nThe Total to pay is: " << char(156) << Total;
           
     cout << "\n\nPlease enter the amount given by the customer: " << char(156);
     cin >> AmountGiven;
     
     ChangeDue = (AmountGiven - Total) * 100;
     
     cout << "\nThe change due is: " << char(156) << ChangeDue << "\n";
     



   getchar();
   getchar();

}

Standish
May 21, 2001

You need to initialise your variables, also learn how to format your code properly and break your lines please.

Also spot the error in these lines:
code:
float PurchaseArray[6] = { 0, 0, 0, 0, 0, 0 };
...
for (int i = 0; i < 10; i++)
{
    PurchaseArray[i]=Sale;
}

MrMartyn
Mar 6, 2007

Thanks very much. It seems to work fine now, pretty simple mistake that I should of spotted. Also the error... should be declared as float instead of int? But I get an error if I do "invalid types 'float[6][float]' for array subscript"

Just out of interest what level of C++ would you say this is? Pretty basic or...?

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


This is fundamental basics, pretty much.

The error here: your array is size 6 (elements 0 through 5), but your loop is iterating and accessing that array from 0 to 9. Six through nine aren't allocated to your array, so you're writing to memory out of bounds, which is a buffer overrun. This has consequences that can be felt immediately or later down the line, depending on which bits of memory got written in the overrun (which could be bloody well anything).

C++ doesn't point this stuff out (some memory libraries do, but that's a debugging function, mainly). You're left to your own devices to make sure you don't overrun arrays, delete memory you didn't mean to, and that sort of fing.

Vinterstum
Jul 30, 2003

MrMartyn posted:

Thanks very much. It seems to work fine now, pretty simple mistake that I should of spotted. Also the error... should be declared as float instead of int? But I get an error if I do "invalid types 'float[6][float]' for array subscript"

Just out of interest what level of C++ would you say this is? Pretty basic or...?

When you declare a static array, what you type inside a bracket can only be a number, which tells the compiler what size the array should be. Writing 'float' doesn't make much sense (which seems to be what you're doing, given the error, but it's generally better if you post the actual code you changed).

And yes, this is very basic C++. No classes, using lots of global variables, messy formatting (primarily due to trying to make pretty text output manually).

Vinterstum fucked around with this message at 23:33 on Mar 3, 2009

weldon
Dec 22, 2006
I've been creating a polynomial class, and have been having trouble on the input operator.

this is what I have so far, but when it prompts, it just spits out the first part of the polynomial
(which isn't part of the loop), or it hangs if i modify the while loop.

My professor wrote down ways to end the loop, but i'm not sure how to implement them in the loop. (she wrote down white space, character not digit, x, ^, +, -)

Am I doing the entire thing wrong? Any ideas? Thanks. :)

The class is called polynomial, and it has two private variables; int d (degree of coefficient), and int * c (coefficient).

code:
//input operator
istream & operator >> (istream & ins, polynomial & P){
  int tempc, tempd, negtemp;
    char xc, c;
    tempc = tempd = negtemp = 0;
      ins >> tempc >> xc >> xc >> tempd;
      if (ins.fail()){
	  cout << "Error! Incorrect format." << endl;
	  return ins;
	}
      delete[] P.c;
      P.c  = new int [tempd + 1];
      P.d = tempd;
      for (int i = tempd; i >= 0; i--){
	P.c[i] = 0;
      }
      P.c[tempd] = tempc;
      c= '+';
	while (!isspace(c) || c ==  'x' || c == '^' || c == '+' || c == '' || isdigit(c)){
	ins.peek();
	if (c == '+'){
	  c = ins.get();
	  c = ins.peek();
	  if(isdigit(c)){
	    ins >> tempc;
	    c = ins.peek();
	    if (c == 'x'){
	      c = ins.get();
	      c = ins.peek();
	      if(c == '^'){
		c = ins.get();
		c = ins.peek();
		if (isdigit(c)){
		  ins >> tempd;
		}
		else {
		  tempd = 1;
		}
	      }
	    }
	    else {
	      tempd = 0;
	    }
	  }
	}
	else if (c == '-'){
	  c = ins.get();
	  c = ins.peek();
	  if (isdigit(c)){
	    ins >> negtemp;
	    tempc = (negtemp) * (-1);
	    c = cin.peek();
	    if (c == 'x'){
	      c = ins.get();
	      c = ins.peek();
	      if(c == '^'){
                c = ins.get();
                if (isdigit(c)){
                  ins >> tempd;
                }
                else {
                  tempd = 1;
                }
              }
            }
            else {
              tempd = 0;
            }
          }
        }
	P.c[tempd] = tempc;
      }
      return ins;
}

Morpheus
Apr 18, 2008

My favourite little monsters
I'm really irritated that I can't figure this out, it's pretty much a first year university question, but I'm pretty rusty when it comes to this stuff (been doing C# a lot lately). I'm trying to write a templated Linked List in C++:

LinkedList.h:
code:
template <class T>
class LinkedList {
	private: 
		LinkedListNode<T>* firstNode;

	public:
		LinkedList<T>();
		
};
LinkedList.cpp
code:
#include "LinkedList.h"

template <class T>
LinkedList<T>::LinkedList(){
	length=0;
}
Seems pretty simple, right? Only trying to instantiate a LinkedList pointer makes a linking error, it says it can't find the constructor. If I put the constructor in the header (ie LinkedList<T>() { length=0; }) creates no error. I just need to know wtf is going on here.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
In brief, you will need to put all of your templated function definitions in the header file; that's just how it works.

Slightly more detail: the compiler needs to know the full definition of the templated function at (basically) every point of instantiation. There is something in the formal spec which achieves this while still kindof-sortof maintaining separate compilation, but it is very rarely implemented.

rjmccall fucked around with this message at 05:37 on Mar 6, 2009

Contero
Mar 28, 2004

Morpheus posted:

it says it can't find the constructor..

All code needs to be in the .h file and you don't need <T> after member functions, but you do need it for the pointer though (I think).

LinkedList.h:
code:
template <class T>
class LinkedList {
	private: 
		LinkedListNode<T>* firstNode;

	public:
		LinkedList();
		
};

template <class T>
LinkedList::LinkedList(){
	length=0;
}

Nippashish
Nov 2, 2005

Let me see you dance!

Morpheus posted:

Seems pretty simple, right? Only trying to instantiate a LinkedList pointer makes a linking error, it says it can't find the constructor. If I put the constructor in the header (ie LinkedList<T>() { length=0; }) creates no error. I just need to know wtf is going on here.

That's pretty much the only way to do it. The compiler needs the code available when the template is instantiated, so it needs to be in the header.

Avenging Dentist
Oct 1, 2005

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

Contero posted:

All code needs to be in the .h file and you don't need <T> after member functions, but you do need it for the pointer though (I think).

LinkedList.h:
code:
template <class T>
class LinkedList {
	private: 
		LinkedListNode<T>* firstNode;

	public:
		LinkedList();
		
};

template <class T>
LinkedList::LinkedList(){
	length=0;
}

Incorrect.

Contero
Mar 28, 2004

Avenging Dentist posted:

Incorrect.

drat. Well you still don't need it after the constructor in the class declaration like he had it. Should be:

code:
template <class T>
class LinkedList {
private: 
   LinkedListNode<T>* firstNode;
   int length;

public:
   LinkedList();
};

template <class T>
LinkedList<T>::LinkedList(){
   length=0;
}

Pooball
Sep 21, 2005
Warm and squishy.

weldon posted:

I've been creating a polynomial class, and have been having trouble on the input operator.

this is what I have so far, but when it prompts, it just spits out the first part of the polynomial
(which isn't part of the loop), or it hangs if i modify the while loop.
What development environment are you using? Can you use a debugger? Otherwise, put in print statements.

quote:

My professor wrote down ways to end the loop, but i'm not sure how to implement them in the loop. (she wrote down white space, character not digit, x, ^, +, -)
Specifically what did she suggest that you could not implement?

quote:

Am I doing the entire thing wrong? Any ideas? Thanks. :)

Write a function that reads one monomial and returns the coefficient and power. Then your polynomial input function just has to call the monomial input function repeatedly. You can test your monomial input separately and fix its bugs.

code:
// If there's a monomial available, reads it,
// sets c and d, and returns true. Otherwise returns false.
bool read_monomial(istream & in, int & c, int & d);
Avoid duplication. You're duplicating most of your code for the "+" and "-" branches. In fact, there are some obvious differences in these bits which must be bugs.

You're using "peek" and "get" inconsistently - why is there a ins.peek() by itself, without the return value being used? (When I stopped the code in a debugger, this is where it was looping.)

Using istream::putback is simpler than using get and peek. So instead of your while loop,
code:
for (;;) {
  c = ins.get();
  if (isspace(c)) {
    // ignore whitespace
  } else if (c == 'x') {
    ...
  } else {
    ins.putback(c);
    break;
  }
}
This way you always consume the input before checking it. No more infinite loops.

Pooball fucked around with this message at 12:02 on Mar 6, 2009

Morpheus
Apr 18, 2008

My favourite little monsters

Folks posted:

lotta helps

Ah, I see...damnit though. This is why I do not like programming in C++ if I can avoid it, this weird poo poo. Oh well.

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?
I've never quite worked out what the scope of a typedef is. If I have a header file myheader.h :

code:
typedef Foo Foo_alias;

void myfunc();
...
then will Foo_alias be visible in any .cpp or .h file that #includes "myheader.h"?

TSDK
Nov 24, 2003

I got a wooden uploading this one

DoctorTristan posted:

I've never quite worked out what the scope of a typedef is. If I have a header file myheader.h :

code:
typedef Foo Foo_alias;

void myfunc();
...
then will Foo_alias be visible in any .cpp or .h file that #includes "myheader.h"?
It gets the same scope as if you'd made any other declaration there, so yes it'll be available in global scope in any .cpp or .h file that includes your header.

weldon
Dec 22, 2006

Pooball posted:

What development environment are you using? Can you use a debugger? Otherwise, put in print statements.
I use emacs and dev-c++. We didn't learn to much about gdb so I can only do the basic functions which don't help too much.
Specifically what did she suggest that you could not implement?
No restrictions except we have to use the class definition given to us

Avoid duplication. You're duplicating most of your code for the "+" and "-" branches. In fact, there are some obvious differences in these bits which must be bugs.
Using istream::putback is simpler than using get and peek. So instead of your while loop,
code:
for (;;) {
  c = ins.get();
  if (isspace(c)) {
    // ignore whitespace
  } else if (c == 'x') {
    ...
  } else {
    ins.putback(c);
    break;
  }
}
im not totally sure where this is going, but the loop is supposed to end at white space, or reading in the wrong character. Should the else if be a +, since its starting a new term? or should i have multiple else if statemnts? When would the for loop end?

Thanks for the help :)

Pooball
Sep 21, 2005
Warm and squishy.

quote:

Should the else if be a +, since its starting a new term?

Possibly. You probably want to handle 'x' as well for the first term and avoid faking a '+' - are you assuming the first term will be positive?

quote:

or should i have multiple else if statemnts? When would the for loop end?
Yes, you can have multiple else if statements. In my example the loop would end when it hits the "break" statement, so whenever it hits a character that isn't matched by one of the "if" conditions.

weldon
Dec 22, 2006

Pooball posted:

Possibly. You probably want to handle 'x' as well for the first term and avoid faking a '+' - are you assuming the first term will be positive?

Yes, you can have multiple else if statements. In my example the loop would end when it hits the "break" statement, so whenever it hits a character that isn't matched by one of the "if" conditions.

Well because the way the array of coefficents are stored (backwards) I read in the first part, make a dynamic array = to its degree, and then keep reading in.

Thus, the loop basicly starts with a + or a -, with no white space. If it is a +, it should be ignored, but obviously if it is a - it probably belongs to a number, so I would want to use it as the coefficient of the following number.

In your example, where would you start to store the variables? You would only want to store them after you have a degree and coefficent that work, and be able to ignore missing ones (ie the 0x^3 from 2x^4+2x^2).

Morpheus
Apr 18, 2008

My favourite little monsters
Me again. Trying to do operator overloading, a subject which I have not a lot of experience in, and I am getting a funky error.

code:
template <class T>
class LinkedList {
	...
  T& operator[](int index);
   ...
}

template <class T>
T& LinkedList<T>::operator[](int index) {
 //Return data here!
  ...
}
So I try to call myList[1], and save it to Thing* (Thing* being what's being stored in myList).

The error?

- cannot convert from 'LinkedList<T>' to 'Thing*'

I don't know wtf. It doesn't even get to my code, it just looks at the one line and says "Well gently caress that".

Morpheus fucked around with this message at 00:36 on Mar 7, 2009

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
That is really not enough information, but I will make a guess anyway. I suppose your code looks like this:

code:
LinkedList* myList = /* blah blah blah */;
Thing* thing = myList[i];
That won't work: this is using the (built-in) operator[] on pointers, not the operator[] on your type. You will need to do one of the following two things:

code:
LinkedList* myList = /* blah blah blah */;
Thing* thing = (*myList)[i];
or

code:
LinkedList& myList = *(/* blah blah blah */);
Thing* thing = myList[i];

Morpheus
Apr 18, 2008

My favourite little monsters

rjmccall posted:

That is really not enough information, but I will make a guess anyway. I suppose your code looks like this:

code:
LinkedList* myList = /* blah blah blah */;
Thing* thing = myList[i];
That won't work: this is using the (built-in) operator[] on pointers, not the operator[] on your type. You will need to do one of the following two things:

code:
LinkedList* myList = /* blah blah blah */;
Thing* thing = (*myList)[i];
or

code:
LinkedList& myList = *(/* blah blah blah */);
Thing* thing = myList[i];

You got it right. Really, I'd need to do that? Hmm. I might as well use .Get(int). Not as nice-looking, but surrounding my list name with parentheses every time I want to use the [] operator, or what you were doing in the second one seem even worse. Thanks for the help! Errors like this are what turned me off C++ in the first place.

Vanadium
Jan 8, 2005

Morpheus posted:

You got it right. Really, I'd need to do that? Hmm. I might as well use .Get(int). Not as nice-looking, but surrounding my list name with parentheses every time I want to use the [] operator, or what you were doing in the second one seem even worse. Thanks for the help! Errors like this are what turned me off C++ in the first place.

You overload operators on your type. You cannot overload operators on pointers to your type, because pointers are a built-in type. This makes a lot of sense and totally does not get in your way if you just used references instead of pointers. :colbert:

Avenging Dentist
Oct 1, 2005

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

Morpheus posted:

You got it right. Really, I'd need to do that? Hmm. I might as well use .Get(int). Not as nice-looking, but surrounding my list name with parentheses every time I want to use the [] operator, or what you were doing in the second one seem even worse. Thanks for the help! Errors like this are what turned me off C++ in the first place.

If you're not clear on the difference between pointer and non-pointer types (and you obviously aren't), you're going to have a hell of a time with C++.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Passing it around by reference (the second option) is pretty reasonable if you get it as a reference in the first place (e.g. you created it as a variable or a member). The only problem is really if you heap-allocated it, but there usually aren't compelling reasons to directly heap-allocate data structures like that.

EDIT: That's not really a defense of this particular obnoxiousness of C++, of course. But having special syntax for it (e.g. Perl's ->[]) would be somewhat terrible, since it would be very easy to accidentally use the wrong syntax — although that would usually be caught by the compiler.

rjmccall fucked around with this message at 01:37 on Mar 7, 2009

Vanadium
Jan 8, 2005

If life gives you pointers, dereference them and make references. Or something.

Morpheus
Apr 18, 2008

My favourite little monsters

Avenging Dentist posted:

If you're not clear on the difference between pointer and non-pointer types (and you obviously aren't), you're going to have a hell of a time with C++.

That's the funny thing, I've programmed with C++ for maybe a year, two years. I stopped for a while, did a lot of C# and Java, and now coming back to this is just a whole lot of playing catch-up with myself, and I'm running into problems I've never run into before for some reason.

UberJumper
May 20, 2007
woop
Right now i am doing a project for my group project class this class is basically heralded as a right of passage for CS students. Were doing this project using Rational Rose (:suicide:).

Anyways right now its telling me that i need to overload the copy operator for this class, except i don't understand why i would need to.

code:
class Block
{
private:
const std::pair<short, short> rep;

public:
Block(const short _value,  const short _rank)
: rep(std::pair<short, short>(_value, _rank)
{}
~Block()
{}
const short rank()
{
return rep.first;
}
const short value()
{
return rep.second;
}
}
Anyways its basically telling me it needs an overloaded copy constructor of this, except i don't quite understand how i can write one for this?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
That code shouldn't compile. You have mismatched parens and no semicolon at the end.

UberJumper
May 20, 2007
woop

Avenging Dentist posted:

That code shouldn't compile. You have mismatched parens and no semicolon at the end.

Sorry, i don't actually have the source of it since rational rose is a UML/automatic code generator/other terrible things. I was basically just trying to turn what i have in their gui into code.

Is it even possible to create a copy constructor really for a class like that though? Since rep is a const pair? It cannot be modified correct? So a copy constructor can't really change anything in this case?

Fullets
Feb 5, 2009

UberJumper posted:

Is it even possible to create a copy constructor really for a class like that though? Since rep is a const pair? It cannot be modified correct? So a copy constructor can't really change anything in this case?

code:
Block::Block(const Block& that) : rep(that.rep) {}

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

UberJumper posted:

Is it even possible to create a copy constructor really for a class like that though? Since rep is a const pair? It cannot be modified correct? So a copy constructor can't really change anything in this case?

You are thinking about an assignment operator, which is not the same thing as a copy constructor.

UberJumper
May 20, 2007
woop

Fullets posted:

code:
Block::Block(const Block& that) : rep(that.rep) {}

That would be perfect. But since rational rose doesnt offer a way to use initalizer lists with copy constructors. Is there someway to get freaky with const'ness and completely remove the constness?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I don't understand your problem. Rational Rose will generate some stubs for you. Then you go in and fill out those stubs. Besides that, the default copy constructor is sufficient, and you don't actually need to type it out.

UberJumper
May 20, 2007
woop

Avenging Dentist posted:

I don't understand your problem. Rational Rose will generate some stubs for you. Then you go in and fill out those stubs. Besides that, the default copy constructor is sufficient, and you don't actually need to type it out.

What do you mean by stubs? If your refering to the generated C++ code, the problem is if i modify those, once i rebuild i will have to go back and rebuild them. Since for this project it is emulating a client/server model using connexis. The applications are run within rose.

Rational rose is basically FUBAR at my university(were using roseRT 1997? i think.) It never works right, and does wonderfully werid things, e.g. demanding a copy constructor or saying the data type for an unsigned int is too complex and needs to be defined manually. I know it should be smart enough to generate one, but for this little example it is not.

Ether way i managed to figure out a way of doing it. Once you define a wonderful copy constructor a magical field under RoseRTS appears! CopyConsturctorInitalizer.

Thanks though.

Chuu
Sep 11, 2004

Grimey Drawer
I have a function:

code:
void doSomething(int x){..}
Currently this code is being called, x is always a literal from an enum.

Is there any way, at compile time, to enforce then x can only be certain values, without breaking interface? Macros are fine.

If I am willing to break the interface, can templates and/or Macros solve this problem easily?

Chuu fucked around with this message at 09:01 on Mar 7, 2009

POKEMAN SAM
Jul 8, 2004

Chuu posted:

I have a function:

code:
void doSomething(int x){..}
Currently this code is being called, x is always a literal from an enum.

Is there any way, at compile time, to enforce then x can only be certain values, without breaking interface? Macros are fine.

If I am willing to break the interface, can templates and/or Macros solve this problem easily?

You can use Boost Static Asserts or roll your own Static Asserts (it's not hard using template trickery.)

Here's the Boost ones, I'm sure you can find an implementation of your own if you look around.

http://www.boost.org/doc/libs/1_31_0/libs/static_assert/static_assert.htm

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

Chuu posted:

I have a function:

code:
void doSomething(int x){..}
Currently this code is being called, x is always a literal from an enum.

Is there any way, at compile time, to enforce then x can only be certain values, without breaking interface? Macros are fine.

If I am willing to break the interface, can templates and/or Macros solve this problem easily?

Why not void doSomething(myEnum x)?

EDIT: Oh also static asserts won't work because the value of x is known only at runtime.

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