daviddoria 334 Posting Virtuoso Featured Poster

The particular case that had caused me problems is this:

typedef itk::Image<Node, 2> ImageType;
itk::ImageRegionConstIterator<ImageType> imageIterator = 

  itk::ImageRegionConstIterator<ImageType>(image, region);

// make a vector of pointers to particular nodes
std::vector<Node*> nodes;
  while(!imageIterator.IsAtEnd())
    {
    nodes.push_back( &(imageIterator.Get()) );
    ++imageIterator;
    }

With the above, I get "warning: taking address of temporary" (and indeed it works incorrectly). The .Get() method of the iterator return a reference to the data at the pixel, so I thought that would be ok, but I guess I don't fully understand references.

My thought was that if the image was Image<Node*, 2> then .Get() would just return the address of the actual node and everything would be ok. Does that make sense?

daviddoria 334 Posting Virtuoso Featured Poster

I have been using a Image<Node> as the main object to store the data for my program. It is a 2D grid of Node objects. The nodes have properties like bool Visited , float Priority , etc.

I often want to modify these nodes, put some of them in a vector to operate on them, pass them around, etc. Lately, it seems like some things would be easier if I had used a Image<Node*> instead.

Is the only overhead that I would have to then iterate through the image and delete all of the nodes when the image is destructed? That is, what are positives and negatives to each approach and when would you use one over the other?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Ah of course - I implemented the assignment operator and all is well.

Thanks!

daviddoria 334 Posting Virtuoso Featured Poster

I realize this question may be impossible, but I'm pretty confused so I'm drawing at straws.

I'm trying to use a class from a big library. If I do this:

TheClass imageIterator(image, region);

everything works fine. However, if I do this:

TheClass imageIterator;
imageIterator = TheClass(image, region);

I get a segfault. The two constructors look like this:

TheClass::TheClass()
{
  m_NumberOfPixelsInRegion    = 0L;
  m_NumberOfSamplesRequested  = 0L;
  m_NumberOfSamplesDone       = 0L;
  m_Permutation = NULL;
}

TheClass::TheClass(const ImageType *ptr, const RegionType & region):Parent(ptr, region)
{
  m_NumberOfPixelsInRegion   = region.GetNumberOfPixels();
  m_NumberOfSamplesRequested = 0L;
  m_NumberOfSamplesDone      = 0L;
  m_Permutation = new RandomPermutation(m_NumberOfPixelsInRegion);
}

It seems to me like calling the default constructor before the second shouldn't change anything, as everything is done in the default constructor is overridden by the second constructor.

From this tiny bit of information does anyone see an issue with doing this?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

If you want to use Qt it look like QConsole should do it: https://sourceforge.net/projects/qconsole/

daviddoria 334 Posting Virtuoso Featured Poster

Here is what I ended up with (I believe it is the same as what FirstPerson said):

struct MyComparison
{
  bool operator()(const Point p1, const Point p2) const
  {
    if((p1.x < p2.x))
    {
      return true;
    }

    if(p1.x == p2.x)
      {
      if(p1.y < p2.y)
        {
        return true;
        }
      }

    return false;
  }
};
daviddoria 334 Posting Virtuoso Featured Poster

Nope, no problem - just more awkward than I'd like :)

daviddoria 334 Posting Virtuoso Featured Poster

But even if the functor class is a nested class of MyClass, it still can't access MyClass's data. See below (particularly the commented line):

#include <iostream>

class Parent
{
  friend class Child;
  int name;
  class Child
  {
    int school;
    void test(Parent* parent) // it is necessary to pass a pointer to the parent when calling this function
    {
      //std::cout << this->name; // this doesn't work - Child doesn't have access to Parent's members
      std::cout << parent->name;
    }
  };
};

int main(int argc, char *argv[])
{
  Parent MyParent;
  
  return 0;
}

Right?

daviddoria 334 Posting Virtuoso Featured Poster

I would have to pass the functor a pointer to the MyClass that it came from, right?

class FunctorBase
{
 void operator() (MessageVector mv, MyClass* parent)
 {
   cout << parent.Data; // even if Data is private to 'parent', this works if FunctorBase is a friend of MyClass
 }
};

Class MyClass
{
 MyFunctor* functor;
 void SomeFunction()
 {
  functor(MyMessageVector, this);
 }
};

Is that what you meant?

daviddoria 334 Posting Virtuoso Featured Poster

Ok, the Boost Function and Bind worked well, thanks!

L7Sqr - True, this doesn't actually help much, you're right. What I was actually hoping for was a way to let people extend my class by providing their own Update() functions in a separate file (i.e. without having to change the MyClass.cxx file directly. That's why I thought the functors were a perfect solution because they could just make a subclass of MyUpdateFunctor and pass it to my class, but the problem is that the update functions need access to MyClass's member data. I guess without some huge design change I'm just out of luck.

Thanks all,

David

daviddoria 334 Posting Virtuoso Featured Poster

@Fbody,

The functions are called:

bool SumProductMessageUpdate(MessageVector& messageVector);
bool MaxProductMessageUpdate(MessageVector& messageVector);
bool MinSumMessageUpdate(MessageVector& messageVector);

and they implement different loops with different equations.

@Mike,

I'm always scared of Boost, but once I figure it out I usually like the result. Here, you have done the figuring out for me, so let me try it :)

daviddoria 334 Posting Virtuoso Featured Poster

Fbody,

They all have the same arguments, so overloading is out :(

daviddoria 334 Posting Virtuoso Featured Poster

VernonDozier,

I was trying not to use function pointers. I've always heard "don't use function pointers" haha - I have no actual reason for not wanting to use them.

daviddoria 334 Posting Virtuoso Featured Poster

They modify their input value (not shown in the example I posted):

void MyClass::Update(double &a)
{
a += 2.0 * this->Data;
}

David

daviddoria 334 Posting Virtuoso Featured Poster

I have a class that has a function called Update(). I have written several different update methods, so I now have:

class MyClass
{
 double Update1();
 double Update2();
 double Update3();
private:
 float Data1;
...
 float DataN;
};

All of these functions need access to all of the data of MyClass. Is there a better way to specify which one of these functions to use than making a

class MyClass
{
 int UpdateMethod;
};

void MyClass::SetUpdateMethod(int method)
{
 this->UpdateMethod = method;
}

double MyClass::Update()
{
 if(this->UpdateMethod == 1)
   return Update1();
 if(this->UpdateMethod == 2)
   return Update2();
...
}

?

I looked into functors (which are pretty cool: http://programmingexamples.net/index.php?title=CPP/Functor) but unfortunately I can't access the data members of MyClass from a functor.

Any suggestions?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Bah, the daviddoria compiler didn't catch the missing return value and apparently I also forgot to turn on -Wall in this demo project.

The crash is gone and it works as I would expect.

I will certainly check out "array views". I need a quick solution for the time being though :)

Thanks!

daviddoria 334 Posting Virtuoso Featured Poster

I am trying to return a vector of some of the elements in a vector. I wish to modify one of the elements in this new vector and have it automatically update the value in the original vector.

I tried this:

#include <iostream>
#include <vector>

class TestClass
{
public:
  TestClass();

  void OutputData();

  //std::vector<double&> GetOddElements(); // can't do this!
  std::vector<double*> GetOddElements();
  
  double& GetElement();

private:
  std::vector<double> Data;

};

/*
// Can't do this!
std::vector<double&> TestClass::GetOddElements()
{
  std::vector<double&> oddElements;
  oddElements.push_back(this->Data[1]);
}
*/

std::vector<double*> TestClass::GetOddElements()
{
  std::vector<double*> oddElements;
  oddElements.push_back(&(this->Data[1]));
}

TestClass::TestClass()
{
  this->Data.push_back(0);
  this->Data.push_back(1);
  this->Data.push_back(2);
}

void TestClass::OutputData()
{
  for(unsigned int i = 0; i < this->Data.size(); i++)
    {
    std::cout << Data[i] << " ";
    }
}

int main(int argc, char *argv[])
{
  TestClass test;
  test.OutputData();
  std::cout << std::endl;

  std::vector<double*> oddElements = test.GetOddElements();
  std::cout << oddElements.size();
  //*(oddElements[0]) = 6;

  test.OutputData();

  return 0;
}

but it doesn't work (it outputs

0 1 2 3
3020671560 1 2 3

when I would expect/want to see:


0 1 2 3
0 6 2 3
)

And it also segfaults upon exit.

Can anyone see where I've gone wrong here?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

You could look into SDL (http://www.libsdl.org/). I know you can play audio files, not specifically sure about mp3s.

Also, please use real English words like "please" instead of "plz". It helps to keep DaniWeb looking professional!

David

daviddoria 334 Posting Virtuoso Featured Poster

I'm assuming you want to take numbers like 10.0 and 9.0 and output them such that they look like

10.0
9.0

If that is correct, it would be a perfect project for you to figure out how to do that and then add it as an example here:
http://programmingexamples.net/index.php?title=CPP

David

daviddoria 334 Posting Virtuoso Featured Poster

You may want to ask this question here:

http://www.qtforum.org/

or on the #qt channel on freenode.net (IRC)

daviddoria 334 Posting Virtuoso Featured Poster

VTK has some Geoviz capabilities: http://www.vtk.org/Wiki/VTK/Examples/Cxx#Geographic_Visualization_.28Geovis.29 but there may be better tools for this job.

daviddoria 334 Posting Virtuoso Featured Poster

A statement like this:

cin >> Total_Number_of_Seconds%3600;

doesn't make any sense. 'cin' is trying to read something from the input stream and store it somewhere. Storing it in "variable%3600" doesn't make sense. Just store it in "variable".

cin >> Total_Number_of_Seconds;

David

daviddoria 334 Posting Virtuoso Featured Poster

That error likely means you are accessing an index outside of the contents of a string:

string a = "hello"; // valid indices are 0-4
cout << a[7]; // should cause that error

Check the bounds of your loops, etc.

David

daviddoria 334 Posting Virtuoso Featured Poster

I'm having trouble with this "strict weak ordering". Consider a class Point with members x and y. I tried to make the comparison "If both the x values are less than each other and the y values are less than each other, the Point's are less than each other". However, in the example below I create two points: (10,10) and (10,11) and the second insert fails.

Can someone please explain my blunder?

#include <iostream>
#include <set>

struct Point
{
  float x,y;
};

struct MyComparison
{
  bool operator()(const Point p1, const Point p2) const
  {
    if((p1.x < p2.x) && (p1.y < p2.y))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
};

int main(int argc, char* argv[])
{
  typedef std::set<Point, MyComparison> SetType;
  SetType s;
  std::pair<SetType::iterator, bool> result;
  Point p1;
  p1.x=10;
  p1.y=10;
  result = s.insert(p1);
  std::cout << result.second << std::endl;
  
  Point p2;
  p2.x=10;
  p2.y=11;
  result = s.insert(p2);
  std::cout << result.second << std::endl;


  return 0;
}

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

So it would be more of an ImageContainer class? Because everything I did would now become

DoSometing(ImageConainter.image)

where it used to just be

DoSomething(image)

That is what you're suggesting, correct?

daviddoria 334 Posting Virtuoso Featured Poster

Ah I see, that makes sense if it was my own image class. However, I am using an already-templated Image class. I was thinking I could just wrap it, but it needs the template argument! If we call it TheirImage, I would want something like:

class MyImage : public TheirImage
{
// implement nothing
};

class MyRGBImage : public MyImage
{
// how to specify that it is a 3 channel image?
};

but it would of course complain that TheirImage needs the template parameter (i.e. TheirImage<N>). Any suggestions?

David

daviddoria 334 Posting Virtuoso Featured Poster

Hm, yea that would work, but as you said doing it like that makes it look like I'm doing it wrong!

Assume the image types did relate - that is, that they are all Image<N> where N is the number of channels (1 for grayscale, 3 for RGB, etc). How would this change anything? Since N is not known until runtime, I have the same problem as before, right?

I'm definitely up for a good redesign, but this was the only way I knew to even get it to work at all (before this problem). Do you have any suggestions? I thought what I was doing was already 'polymorphism'?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

What I really wanted was a single class, ImageSegmentation<T> that operates on images of type T. The problem was that this object needed to be a member of my Qt form object (called Form), but I didn't know T until runtime (the user selects which type of image to operate on. Because of that, I structured it like this:

ImageSegmentationBase - functions that don't depend on T
ImageSegmentation<T> : public ImageSegmentationBase - function which depend on T.

Then I member variable of Form:

ImageSegmentationBase* MyImageSegmentation;

I instantiate it with

MyImageSegmentation = new ImageSegmentation<T>;

The problem now is that I can't get the data out! From a function Form::function() I want to do:

Form::function()
{
T image = MyImageSegmentation->GetData();
... continue processing image normally...
}

Of course GetData can't be defined in ImageSegmentationBase because the return type is T. It can be defined no problem in ImageSegmentation<T>, but I don't know T inside Form::function()!

I tried to put a typedef inside ImageSegmentationBase:

template<class T>
struct ImageSegmentationBase{
  typedef T type;
};

To then do

Form::function()
{
MyImageSegmentation::type image = MyImageSegmentation->GetData();
}

but it says "ImageSegmentation is not a namespace".

Is there a way to do something like this? It seems like a pretty standard pattern, no?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Have you gone through the tutorials here : http://nehe.gamedev.net/ (on the left side there are 52 OpenGL tutorials.)

Once you distill some of that, if you could add some short code snippets here which demonstrate very particularly functionalities:
http://programmingexamples.net/index.php?title=OpenGL

that would be a great exercise for you and very helpful for everyone else :)

David

daviddoria 334 Posting Virtuoso Featured Poster

I don't follow you, can you clarify? What are you trying to do?

daviddoria 334 Posting Virtuoso Featured Poster

Unfortunately I don't know Java, but you can get a good idea from here:
http://www.cplusplus.com/reference/stl/vector/

If you initialize it like you have done, it will have 0 elements. Using push_back(element) will extend the vector and insert the element in the last position. Once the vector has some contents, you can access elements with initial.

Several other things you can do with vectors are shown here:
http://programmingexamples.net/index.php?title=CPP/STL/Vector

David

daviddoria 334 Posting Virtuoso Featured Poster

Hi Banh, welcome to DaniWeb!

The istringstream is a stringstream - it lets you "manipulate strings as if they were input/output streams."
http://www.cplusplus.com/reference/iostream/stringstream/

You don't show how you have defined 'initial', but atoi could probably be replaced by another stringstream operation.

Also, please use code tags when posting code.

David

daviddoria 334 Posting Virtuoso Featured Poster

thelerner - Please mark the thread as solved. It helps so that the next reader does not read through your question only to find that it is no longer a question!

David

daviddoria 334 Posting Virtuoso Featured Poster

On line 57 'unique' is undefined.

If I define it, then I get:

In function ‘int unique_multiD(double**, int, int)’:

warning (really, error!): no return statement in function returning non-void
daviddoria 334 Posting Virtuoso Featured Poster

stankefa,

Welcome to DaniWeb!

Try commenting line 23 and 24. You should see only the copy constructor called. On line 23, you are calling f(), so of course it will be called!

Also, in the future, please use a descriptive thread title - for this question maybe something like "Copy constructor problem".

David

daviddoria 334 Posting Virtuoso Featured Poster

"calling the condition function gives me error" - what is the error? There should be no problem calling a function from another function.

daviddoria 334 Posting Virtuoso Featured Poster
for (i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
     cout<<list[i][j] << " "; // put a space between the characters on the same line
}
cout << endl; // start the next line
}
daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb. To help us help you most effectively, please use a descriptive thread title. Please also use code tags when you post code.

It looks pretty "compact" (aka hard to read!) already to me!

David

daviddoria 334 Posting Virtuoso Featured Poster

Yikes! Never, ever, ever use goto! Also, this is going back and opening the file again - is this what you want?

daviddoria 334 Posting Virtuoso Featured Poster

You should also look at making a std::vector<std::string> .

daviddoria 334 Posting Virtuoso Featured Poster
#include <iostream>
#include <string>

int main()
{
  std::string string1 = "Hello";
  string string2 = "World";

  // Concatenate strings together using the operator +
  string full = string1 + " " + string2;

  std::cout << full << std::endl;
  return 0;
}
daviddoria 334 Posting Virtuoso Featured Poster

You should just use a normal std::string object. You can concatenate to it using the + operator:
http://programmingexamples.net/index.php?title=CPP/Strings/Concatenate

daviddoria 334 Posting Virtuoso Featured Poster

Why are you using

string term[]={"Jordan"};

? Why not just

string term = "Jordan";

Also, why do you have quotes around

"term[i]"

?

daviddoria 334 Posting Virtuoso Featured Poster

Please post your code directly using code tags. Preferably, simplify the code as much as possible so that it only demonstrates the relevant parts.

daviddoria 334 Posting Virtuoso Featured Poster

What is the problem? Does it crash? Give the wrong result?

daviddoria 334 Posting Virtuoso Featured Poster

You should definitely check out Qt.

I have an introduction here:
http://rpi.edu/~doriad/Talks/Qt_Introduction.pptx

and a bunch of examples here:
http://programmingexamples.net/index.php?title=Qt

It will keep you pretty occupied!

David

daviddoria 334 Posting Virtuoso Featured Poster

You should really really learn to use a debugger. Almost every IDE has one built in.

Also, if your problem is solved please mark the thread as 'solved'.

daviddoria 334 Posting Virtuoso Featured Poster

1) I've seen something like this before:

class OSG_EXPORT Group : public Node

to indicate that the class is part of a specific library ( I think used for wrapping by other languages?)

2)

Group(const Group&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);

I believe the first parameter is required and the second is optional, with a default value of CopyOp::SHALLOW_COPY (probably an enum).


3)

META_Node(osg, Group); // what is META here?

I don't think META is separate from META_Node. This is probably a function or a macro.

David

daviddoria 334 Posting Virtuoso Featured Poster

Please mark the thread as solved.

daviddoria 334 Posting Virtuoso Featured Poster

1) Please use real English words - I.e. "please" instead of "plz".

2) You should be able to produce these same errors with about 10 lines of code, we do not want to look at 1200!!

3) These missing header files means your system either does not have those files or your include paths are not setup properly. Which compiler/operating system/IDE are you using?