Banfa 597 Posting Pro Featured Poster

So firstly in my above example I called MoveRect on the object Rect but is sounds like you need to do it on Rect2. Which object have you called MoveRect on?

What do you not understand about the operation of the new MoveRect? You have plenty of other member functions, it works in exactly the same way they do.

I can't help you much with the intelisense, that is a feature of the IDE you are using and I do not use MSVC on the whole. Surfice it to say that the intelisense not working correctly should not stop you programming (although it may be slightly frustrating). Often things like intelisense do not work properly in the presence of compielr errors. have you tried

Exiting you IDE and restarting it
Performing a project rebuild all

Banfa 597 Posting Pro Featured Poster

MoveRect needs to be called on a rectangle object because it is now a class member Rect.MoveRect(10, 30);

Banfa 597 Posting Pro Featured Poster

now , this program is printing all the prime numbers < 15 ,

but it is printing them more than one time...why ? every prime number is printed there but since 9 is not a prime number why it is printed ?

No it doesn't for instance it prints 9 which is definately not prime.

You still have your print statement inside your inner loop. it needs to be outside your inner loop.

Banfa 597 Posting Pro Featured Poster

i already have my program to check that the input number is prime or not and your program is for the specific number while i want to calculate first 20 primes.
and here is what i've made but it is not working,

int main(void)
{
int a,b,c;
for (a=1;a<21;a++)
{
for (b=21;b>2;b--)
if (b<a)
{
c=a%b;
if (c==0)
break;
else
printf ("%d",a);
}
}
getche ();
}

You want to print the prime numbers, however the test for primality is not divisible by any integer less than itself except 1. That means you have to check every single integer (not true actually but in your simplistic algorithm it is) before you can be sure a number is prime. That means your print statement must be outside the inner loop and when you have exited that loop you need to be able to tell if the loop completed or if the break happened. If the break happened the number is not prime.

Also note your end condition for inner loop is wrong, you never check to see if a is divisible by 2. I think you may find that 4 is incorrectly identified as prime.

You say you want the first 20 primes but this actually calculates the number of primes <= 20, there will certainly not be 20 of them. The end condition for the outer loop should be on the number of primes found so far.

Xufyan commented: thankx..xufyan +0
Banfa 597 Posting Pro Featured Poster

lines 117 - 123, when I said re-write the function without the parameter and operate directly on the class members that did mean that you would have re-write the function to remove references to the parameter aRect and instead use references to the member variables of the class, not that you could just change the function declaration.

Re-write the function using member variables just as all the other class member functions are using member variables.

Banfa 597 Posting Pro Featured Poster

don't move the } at line 93 just change line 92 from friend void MoveRect(Rectangle& aRect, int x, int y); to void MoveRect(int x, int y);

Banfa 597 Posting Pro Featured Poster

Line 115 re-write

// Friend function to move rectangle
void MoveRect(Rectangle& aRect, int x, int y)
{
    ... function code snipped 
}

as

// Member function to move rectangle
void Rectangle::MoveRect(int x, int y)
{
    ... function code snipped 
}
Banfa 597 Posting Pro Featured Poster

no you would remove the first parameter as well and operate directly on all the class members just like your other member functions.

Banfa 597 Posting Pro Featured Poster

Like the error says you can not use the friend keyword in a managed type (manged C++ has delegates and events to get round that).

Why can you just make your MoveRect function a class member?

Banfa 597 Posting Pro Featured Poster

<pedantic>
Most of the above posts make the following assumptions

  1. 'B' - 'A' == 'C' - 'B' == ... == 'z' - 'y' == 1 and similarly for all letters that are adjacent in the alphabet.

  2. 'a' - 'A' == 'b' - 'B' == ... == 'z' - 'Z' and similarly for all lower case upper case letter pairs in the alphabet.

These relationships between the numeric values of letters are not a function of C (or C++) but rather a function of the execution character set in use, in this case ASCII. The C standard does not require the use of ASCII as the execution character set or in fact any particular character set and does not require the relationships 1 and 2 above. It does require that

'1' - '0' == '2' - '1' == '3' - '2' == '4' - '3' == '5' - '4' ==
'6' - '5' == '7' - '6' == '8' - '7' == '9' - '8' == 1

There are some character sets where the letters do not have contiguous character values EBCDIC is the example normally given where 'J' - 'I' == 8.

While a very large number of today's platforms use ASCII as their compilation and execution character set there are still some platforms (mobile phones spring to mind, or at least the SMS parts of them) that don't.

It is normally easy enough to get round this …

Banfa 597 Posting Pro Featured Poster
int number = 65;
char letter = number;

There are no characters in C only integers with different ranges. A character is no more than a number that is interpreted by the display sub-systems to write pixels on the screen in a forum that you recognise as a letter.

Banfa 597 Posting Pro Featured Poster

Just put an if statement round the contents of the inner loop testing the condition a != b

BTW the is little point doing the calculation for b == 1 because a % 1 == 0 for all a.

Banfa 597 Posting Pro Featured Poster

You've missed the braces { } round the 2 lines of code that should be in the inner loop so that the printf statement is only executed once for every iteration of the outer loop.

Banfa 597 Posting Pro Featured Poster

Firstly if cpu utilisation is only getting to 70% there is nothing to worry about. All programs are getting all the cpu time they require with time (30% of the available time) left over.

A single run of your program however only opens a single file and reads it, if you are talking about multiple files then you must be running the program multiple times. It is the operating systems responsibility to share processor time between process that need it even if some of those processes are demanding 100% of the cpu time the operating system will on the whole not them them have it.

If you are still worried then are are things you can do but they will slow down the operation of the program.

You could run the program at a lower priority. The priority is an attribute that the operating system uses when scheduling tasks, lower priority programs get less processor time. You can change the priority either programatically by making approriate function calls or by using a utility like nice (try "man nice") to run the program with a reduced priority.

You could interrupt the read loop in the program with a yield or a small sleep.

However I would recomend that you do neither of these until you get to the point where you computer is getting problems because it is running out of processing resource which is isn't currently, it still has 30% of that resource left.

Banfa 597 Posting Pro Featured Poster

If you put in flagGroup to try and set the size of the bitfield then forget it. You need to use the correct type in the bit flied itself.

Banfa 597 Posting Pro Featured Poster

I get this error from your code using gcc

BitFlags.h:12: error: ISO C++ prohibits anonymous structs

What compiler are you using, this is improtant of course because bitfield behaviour is very much platform dependent.

Anyway after naming the struct I get a size of 4 for the struct as you have it defined.

if you want the bitfield size to be 16 bits you need to declare it unsigned short or if you want to use negative values short .

I ended up with this

union bitFlags {
	unsigned short flagGroup; //provide a 16-bit backbone (on 32-bit systems)
	struct { //the bit field
		signed short multiplierRule : 2; //multiples-of-a-kind rule,
		//"on" causes values to be 2x,3x,4x three-of-a-kind value resp.
		unsigned short straightRule : 1; //"on" activates bonus for a straight in a single roll
		unsigned short threePairRule : 1; //"on" activates bonus for three pairs in a single roll
		unsigned short hardWinRule : 1; //"on" activates the hard win rule-set
	} s; //close the bit field structure
}; //close the union

P.S. I would have thought you should know that "it wont compile" is a bit useless without the actual compiler errors :p

Banfa 597 Posting Pro Featured Poster

Not for template functions (and classes).

A template function are the instructions to the compiler on how to write the function given the parameter types... the template of the function.

When the compiler compiles your code and sees a call to a template function it looks up the template function and uses it to create an actual function that it compiles into the object for the file.

However the compiler works on a single file at a time. If the template function is not defined somewhere in the file being compiled the compiler can not create a real function from the template when it sees the function call because it can not see the template.

So the definition of the template function needs to be visible to the compiler when it is compiling your file there are 2 ways of doing this

  1. Put the definitions of the template functions in the header.

  2. Put the definitions of the template functions in a different file, NOTE not a cpp file because best practice is not to include cpp files, and then include that file into the header.

Whatever you do at the time the compiler sees the template function call it must have already seen the definition of the template.

hkBattousai commented: Thank you for the explanation. +4
Banfa 597 Posting Pro Featured Poster

No if you take your lib you will be fine.

If you took your header file in its original form with no C file and no lib then you would get the error I described.

Banfa 597 Posting Pro Featured Poster

What hasn't been mentioned yet is if you left the function defined in the header file and then included that header into 2 C files in a project and tried to compile and link those 2 files you would get a "symbol defined multiple times" error from the linker because the function that is in the header would be present in both object files when you try to link them and the linker is unable to determine which one of the 2 functions to use.

Every function must appear in a program precisely 1 time. For that reason you must avoid putting functions in headers and for the same reason you must avoid defining variables in headers.

Banfa 597 Posting Pro Featured Poster

All your #defines have a ; at the end of them which will be prducing invalid syntax, remember a #define is a text substitution.

Also you should note define functions, like snapwrite, in a header. You should declare the function in the header

int snapwrite(char *_symbol, int _typeval, char *_val);

and define the function in a C file somewhere.