daviddoria 334 Posting Virtuoso Featured Poster

I was wondering if I partially specialize a class (which I'm understanding to mean hardcode some of the template parameters?) if I have to implement all functions or if I can just implement some functions. I put together a demo:

http://programmingexamples.net/index.php?title=CPP/Templates/PartialClassSpecialization

Where in the main class definition there are two functions, and then only one of them gets specialized in the <T,int> specialization.

However, I'm getting an error which I don't believe has to do with my original question :

on this line

class Point<T, int>

I get

Point.h:16: error: template parameters not used in partial specialization:
Point.h:16: error:         ‘<anonymous>’

I assume it's just a syntax problem? But for some reason I haven't been able to find a clear demonstration of this.

Can anyone see the problem?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

It depends on what you want to do with it. If you want to store it exactly like you see it, store it in an std::string. If you are doing to need to do operations on it, you would need to choose an appropriate data type.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags when posting code. Also, try to remove any code that is not related to the problem. For example here the greeting() function is unnecessary. You will find that the shorter you can make the problem, the more likely people are to look at it :)

daviddoria 334 Posting Virtuoso Featured Poster

Please mark this thread as solved since WaltP separated it.

daviddoria 334 Posting Virtuoso Featured Poster

I think

for (; num; num /= base) {

could be written

for (; num != 0; num /= base) {

It was using the idea of "anything non zero evaluates to true (or "continue the loop") while when it is zero the loop should stop.

So basically the loop is dividing 'num' by 'base' over and over until the division yields zero.

This line:

result.insert(result.begin(), digits[abs(num % base)]);

Just puts the number digits[abs(num % base)] on the end of the string.

David

daviddoria 334 Posting Virtuoso Featured Poster

Please include the entire compiler output. Which identifier is not found? My guess would be read_file, since you didn't declare it before main.

daviddoria 334 Posting Virtuoso Featured Poster

You may want to take a pass through something like this:
http://www.cplusplus.com/doc/tutorial/

to get an idea of variable types, etc.

daviddoria 334 Posting Virtuoso Featured Poster

For starters, I would suggest never using spaces in a file name :)

If you insist on doing so, I think Windows likes backslashes instead of forward slashes - I don't if c++ cares about this.

Also, you may need to "escape" the space with a backslash:
read_file("F:/2nd\ year_2/Data structure 2/books/lecture-26.pdf");

daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb! To help us help you, here are some tips:

1) Use code tags (I see you put brackets around the code, but you need to use code goes here [/code ] (but without the space)

2) Please tell us what the program is supposed to do. We can't know what is going wrong if we don't know what the result is supposed to be. Show us a sample input (or see #3 below), the current output, and tell us the expected output.

3) I always suggest hardcoding values to make sure there is no problem with your user input. Once it works as you would expect with a hardcoded value, you can then test the user input.

Good luck,

David[code ]code goes here [/code ] (but without the space)

2) Please tell us what the program is supposed to do. We can't know what is going wrong if we don't know what the result is supposed to be. Show us a sample input (or see #3 below), the current output, and tell us the expected output.

3) I always suggest hardcoding values to make sure there is no problem with your user input. Once it works as you would expect with a hardcoded value, you can then test the user input.

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

This should get you going :)

http://tinyurl.com/6jtjads

daviddoria 334 Posting Virtuoso Featured Poster

Thanks all. I thought this might be the case, but I thought I'd double check.

daviddoria 334 Posting Virtuoso Featured Poster

You'll have to provide more information. Do you really need to store all 1000 of these 3000x3000 matrices? If so, then you're just out of luck :). Give us an explanation of the algorithm.

daviddoria 334 Posting Virtuoso Featured Poster

Yea if its getting to 95% right before it crashes that is definitely an "out of memory".

daviddoria 334 Posting Virtuoso Featured Poster

I think you should verify that it is indeed an "out of memory" before we look into it too far. I can't give you exact instructions because I'm on linux at the moment, but I believe if you open the task manager there should be some plots/statistics about cpu/memory usage. I'd leave that open while you run the program and if it goes up and up and up and gets to the top and then crashes then you've found the problem :)

daviddoria 334 Posting Virtuoso Featured Poster

Are you deleting the memory you are allocating with malloc? Depending on how big these matrices are, maybe you are running out of memory? Do you need to keep each of these 4000 matrices? Or just use it and then discard it?

daviddoria 334 Posting Virtuoso Featured Poster

1) Please use code tags.
2) Use <iostream> instead of <iostream.h> (and similar for the other headers)
3) Use more descriptive variable names. What are x? y? i?
4) You'll have to give us a better idea of the problem. What is the current output? What is the expected output?

daviddoria 334 Posting Virtuoso Featured Poster

Is there a way to make a string variable in "normal" html (that is, not inside of a <script> tag)? I want to do something like

Number='005'
<a href="http://domain.com/"+Number+".html">my link</a>

Is this possible?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

I suggest you make a much much shorter example (20 lines instead of 200). The inputs should be hardcoded. You should be able to clearly demonstrate and show us the input, the current erroneous output, and the expected output.

daviddoria 334 Posting Virtuoso Featured Poster

Before messing around with a printBoard() function, you should make some examples for yourself to make sure you understand the basics. Create a string and output it. Does it look correct?

daviddoria 334 Posting Virtuoso Featured Poster

It is highly unlikely that anyone is going to do this for you. You should instead ask "How do I do this?". The c++ forum is probably not the right place for this question anyway. Which GUI framework are you using?

daviddoria 334 Posting Virtuoso Featured Poster

Before messing around with tictactoe, you should make some examples for yourself to make sure you understand the basics. Create a string and output it. Does it look correct?

Stringstream works just like "cin" (an input stream). Here is a demo:
http://programmingexamples.net/index.php?title=CPP/StringStream

David

daviddoria 334 Posting Virtuoso Featured Poster

I think your question would get better responses here: http://forums.libsdl.org/viewforum.php?f=1

daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb! TO allow us to most effectively help you, please use code tags when you post code.

Why are 'A' and 'result' their current size? Shouldn't they be much smaller if the code was written explicitly for 3x3 matrices?

Also, about half of the code you have posted is for user input. I suggest hardcoding values that you know the result of into the program for testing. This will allow you (and us) to compile and test much more quickly. You can add the user input back when you are confident it is working correctly.

David

daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb! To allow us to help most effectively, please use code tags when you post code. Also, you should always comment code as much as you can. When you post code that isn't working, you should also show the incorrect output (or any errors that occur). I also suggest that you make a fully compilable example that demonstrates the problem (i.e. include main(), and hardcode values rather than taking user input).

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

gcc 4.4

daviddoria 334 Posting Virtuoso Featured Poster

Haha you're right - typo in the demo - no wonder it worked for you Fbody :) In the real situation the child is indeed derived from the parent.

daviddoria 334 Posting Virtuoso Featured Poster

Hm, no it looks exactly like the demo as far as I can tell. Strange - I fixed it by making it pure virtual and putting the "nothing" implementation in other subclasses (I don't have time to really look into it now). I just wanted to see if there was some rule about "if you define it inside the class declaration then virtual doesn't work" or something. I guess not.

daviddoria 334 Posting Virtuoso Featured Poster

If I have this structure:

class Parent
{
public:
 virtual void MyFunction() {// do something}
};


class Child
{
public:
 virtual void MyFunction() {// do something else}
};

The Parent::MyFunction() seems to always be called, even with:

Child MyChild;
MyChild.MyFunction();

Is this expected?

David

daviddoria 334 Posting Virtuoso Featured Poster

1) Be very careful, 'word' may be a reserved name on some systems.

2) You have declared an array of strings for 'guess' but I think you just want it to be one string? As Chilton pointed out, you have done this cin >> guess[50]; when I think you just wanted cin >> guess[0]; which really means you should have done:

string guess;
...
cin >> guess;

David

daviddoria 334 Posting Virtuoso Featured Poster

You seem to have defined the add() function but not a unian() function? If this is just a typo, please post code that should compile so we can see the whole structure.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags when posting code. Also, what is the problem? Please give us example input, current output, expected output, any errors, etc.

daviddoria 334 Posting Virtuoso Featured Poster

Here is an attempt at a demo. You can see the error on the << operator - how would I output the value in 'numbers' at the location pointed to by the Rgb pointer?

#include <iostream>

class Rgb
{
public:
  static int* Channel;
  static void SetChannelSize(unsigned int channelSize)
  {
    Channel = new int(channelSize);
  }
};

int main()
{

  int numbers[1000];
  for(unsigned int i = 0; i < 1000; i++)
    {
    numbers[i] = i;
    }

  Rgb::SetChannelSize(3);

  Rgb* rgbNnumbers = reinterpret_cast<Rgb*>(numbers);
  
  for(unsigned int i = 0; i < 5; i++)
    {
    std::cout << rgbNnumbers[i] << std::endl; //error: no match for ‘operator<<’ in ‘std::cout << *(rgbNnumbers + ((unsigned int)i))’

    }
  
  return 0;
}
daviddoria 334 Posting Virtuoso Featured Poster

Mike, that was the first thing I thought of, but I didn't know how to do that so that it would happen for not a specific object. That is, how do you do something like that so that the general type of RGB changes size, not just a specific instance of the object (i.e. before it was a static property, so all RGB's were affected, not just the one you called the constructor on)?

daviddoria 334 Posting Virtuoso Featured Poster

It looks like you missed copying/pasting the critical line to answer this question! (The function declaration is missing!)

daviddoria 334 Posting Virtuoso Featured Poster

>>is there anyway I can change this Rgb type to now have the desired stride length?

No. All types in C/C++ have a fixed size, that cannot be changed at run-time. No way.

If you could change the interface, it would be better to output an iterator object which then stores a stride which could vary at run-time (but, of course, it would make the increment/decrement operations much slower).

Another option is to have another function like "getRgba" that returns a 4-byte stride pointer instead. If the user attempts to call getRgb with an image that has 4 bpp, then you throw an exception or return a NULL pointer. Of course, this isn't very nice on the user-side, but that's life.

Hm, the iterator is interesting, but that would require massive work to change the existing code.

The getRgba() function sounds easier, but the problem with this is that the code currently does things like:

double Energy(Rgb* A, Rgb* B)
{
double sum=0;
for(unsigned int i = 0; i < 3; i++)
  sum+=A[i] - B[i];
}

Would I have to overload every function that accepts a Rgb*? And what about functions that use them internally?

double OtherFunction(Image* image)
{
Rgb* A = image->GetRgb();
}

?

(Sorry, as usual I am being annoyingly vague because the problem is very large and I'm trying to simplify it down to something post-able here. I really like just hearing a bit of discussion about the problem to try to nudge …

daviddoria 334 Posting Virtuoso Featured Poster

I am working on some code (not written by be, and too big to fundamentally change the structure of) where a pure virtual Image class is implemented. The user is expected to implement a function of their derived Image class with signature:

virtual Rgb* GetRgb();

Where the Rgb type is defined as:

struct Rgb
{
  static const int NUM_CHANNELS = 3;
  unsigned char channel[NUM_CHANNELS];
};

This Rgb type is never instantiated directly, it is simply used to determine the stride length of the pointer through the image data.

If I read a file and determine the pixels have more than 3 components, is there anyway I can change this Rgb type to now have the desired stride length, for example, unsigned char * 4 ? Note: templates are not an option (design decision not made by me).

Any helpful hints?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

You should use more descriptive names. What is 'sz'? What does 'calc' do?

You should also post the shortest compilable example you can make that demonstrates the problem.

daviddoria 334 Posting Virtuoso Featured Poster

I suggest creating a function called OutputBinary(int lengthOfNumbers) and testing it by hardcoding a call to OutputBinary(3). This way you will be able to tell if the problem is with your output of if it is with your user input.

daviddoria 334 Posting Virtuoso Featured Poster

Haha, you will quickly learn that just because your instructor does it doesn't mean it is right :) I'd follow jonsca's advice on this one.

daviddoria 334 Posting Virtuoso Featured Poster

Sreekiran,

As a small side note, please use real English words like "please" instead of "plz" - it helps keep Daniweb looking professional!

daviddoria 334 Posting Virtuoso Featured Poster

I suggest using a debugger to step through the code. It will show you exactly where/when the program crashes, as well as you can look at the conditions (values of variables, etc) when it occurs.

David

daviddoria 334 Posting Virtuoso Featured Poster

For Linux, use gcc/g++. Then you will probably want an IDE such as KDevelop.

daviddoria 334 Posting Virtuoso Featured Poster

You can look into SDL - http://www.libsdl.org/.

daviddoria 334 Posting Virtuoso Featured Poster

I suggest simplifying this code as much as possible. That is, we don't need to see the encryption function at all to look for this problem. Typically by doing this, you will be able to see the problem yourself :)

Also, explain the input, expected output, and current output.

daviddoria 334 Posting Virtuoso Featured Poster

I would start by switching from char arrays to std::string and from strcmp to .compare - http://programmingexamples.net/index.php?title=CPP/Strings/Compare
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines

daviddoria 334 Posting Virtuoso Featured Poster

Can you show us both the working c command and the non-working c++ command?

daviddoria 334 Posting Virtuoso Featured Poster

I suggest trying to boil down your problems into very small demos. In many cases it will help you identify what is going wrong enough that you can solve it yourself. If not, it will produce a helpful question/answer pair for DaniWeb. Once you figure it out, if you don't already see something similar here: http://programmingexamples.net/index.php?title=CPP I would encourage you to add an example.

David

daviddoria 334 Posting Virtuoso Featured Poster

I think you want the -l flag of g++.

daviddoria 334 Posting Virtuoso Featured Poster

I think you're missing two semicolons:

#include <iostream>

class Hop
{
protected:
  static struct NStrct
  {
    int nCount;
  } test;
};

int main()
{

  return 0;
}
daviddoria 334 Posting Virtuoso Featured Poster

Mike,

Yea you're right! I didn't see the Value function. It does just what I was looking for. Thought it seems inappropriately named... I would have said something more like GetReference().

Thanks!