daviddoria 334 Posting Virtuoso Featured Poster

Which operating system are you using? Which IDE are you using (visual studio, KDevelop, etc)?

daviddoria 334 Posting Virtuoso Featured Poster

Ok, I found it. This is quite annoying... but there are 3 solutions (I got most of them from here: http://tuxdna.wordpress.com/2010/07/17/c-inheritance-and-function-overloading/)

1) Write using Parent::Add; in the derived class: (I show two derived classes to demonstrate why you would actually want to do this in the first place)

#include <iostream>

class Parent
{
  public:
    virtual int Add(int a, int b) = 0;

    int Add(int a)
    {
      return this->Add(a,a);
    }
};


class Child1 : public Parent
{
  public:
    using Parent::Add;
    
    int Add(int a, int b)
    {
      return a+b;
    }
};

class Child2 : public Parent
{
  public:
    using Parent::Add;

    int Add(int a, int b)
    {
      return a*b;
    }
};


int main()
{

  Child1 test1;
  std::cout << test1.Add(3) << std::endl;

  Child2 test2;
  std::cout << test2.Add(3) << std::endl;

  return 0;
}

2) Use this syntax std::cout << test1.Parent::Add(3) << std::endl;

#include <iostream>

class Parent
{
  public:
    virtual int Add(int a, int b) = 0;

    int Add(int a)
    {
      return this->Add(a,a);
    }
};

class Child1 : public Parent
{
  public:

    int Add(int a, int b)
    {
      return a+b;
    }
};

int main()
{

  Child1 test1;
  std::cout << test1.Parent::Add(3) << std::endl;

  return 0;
}

3) Define the function (that should be inherited...) to simply call the parent function:

int Add(int a)
    {
      Parent::Add(a);
    }
#include <iostream>

class Parent
{
  public:
    virtual int Add(int a, int b) = 0;

    int Add(int a)
    {
      return this->Add(a,a);
    }
};

class Child1 : public Parent
{
  public:

    int Add(int a, int …
daviddoria 334 Posting Virtuoso Featured Poster

nezachem - no, I am trying to call the 1 argument version that should be inherited.

AidySun - I tried that and it didn't change anything.

Also note - if I change the one argument Add function to be called Test, everything works fine... this is very strange.

daviddoria 334 Posting Virtuoso Featured Poster

Can anyone explain why this doesn't work?

#include <iostream>

class Parent
{
  public:
    virtual int Add(int a, int b) = 0;

    int Add(int a)
    {
      return this->Add(a,a);
    }
};

class Child : public Parent
{
  public:
    int Add(int a, int b)
    {
      return a+b;
    }
};


int main()
{

  Child test;
  std::cout << test.Add(2);

  return 0;
}

It says:

error: no matching function call to 'Child::Add(int)'

The Child instance should be able to see the one argument Add function because it is inherited, right??

If I change the Child class to

class Child : public Parent
{
  public:
    int Add(int a, int b)
    {
      return a+b;
    }

    int Add(int a)
    {
      Parent::Add(a);
    }
};

It works fine. I thought the whole idea of inheritance was that I didn't specifically have to tell the derived classes about these functions?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags when you post code. Also, what is that? What is the problem?

daviddoria 334 Posting Virtuoso Featured Poster

You are correct - typically you give them the source code and they compile it themselves. If you link against any libraries, they would have to have identical versions of the libraries to run the binaries directly. You would also have the issue of 32 vs 64bit.

daviddoria 334 Posting Virtuoso Featured Poster

I am also confused and don't know what to do... haha. Can you post the problematic code? It is probably going to be hard for us to debug without access to a big cluster.

daviddoria 334 Posting Virtuoso Featured Poster

Is this a compiler error? It seems to be related to your development environment more so than your code...

daviddoria 334 Posting Virtuoso Featured Poster

Can you please post the code that is generating this error?

daviddoria 334 Posting Virtuoso Featured Poster

And if you're going to cut and paste a whole assignment, at least make the the formatting is not all wacky! Also, you don't seem to be asking for project ideas, but rather how to do a specific assignment!

daviddoria 334 Posting Virtuoso Featured Poster

And as far as lay-people are concerned, Ubuntu IS Linux. You can't just install Linux, you have to install one of its varieties (Ubuntu, Fedora, etc).

daviddoria 334 Posting Virtuoso Featured Poster

'std::out_of_range' means you are accessing an element of the vector that does not exist.

Here is my suggestion. If the input is working properly, then hardcode some values for the input. Then post the shortest compilable code that demonstrates the problem (hopefully < 20 lines). This makes it very easy for us to see what is going on (without having to download 5 files and look through a bunch of code that likely doesn't have anything to do with the problem).

David

daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb!

First please use more descriptive thread titles :)

Second, that is pretty much a signal processing question - not really a c++ one. If you find an algorithm and try to implement it in c++ and have trouble, then we may be able to help. I would strongly suggest finding an audio processing library instead of trying to write readers/writers/filters yourself.

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

Can you post some compilable code (i.e. which headers are you using?) Have you output the value of GetTickCount() before and after the algorithm?

daviddoria 334 Posting Virtuoso Featured Poster

Don't forget to close your code tag :)

First, there is no reason to use global variables here. It is just a bad habit that will bite you at some point. Second, you are asking it to read 'a' lines (477). The example data you posted is only a few lines, so it just dumps a bunch of garbage. You should definitely read until the end of the file - not an arbitrary number of lines.

You could read all of the lines like this:
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines

then use a std::stringstream to parse it.

Hope that helps,

David

daviddoria 334 Posting Virtuoso Featured Poster

Please use real words. 'plssss' and 'tnx' are not words! It helps to keep the community looking professional.

Ezzaral commented: Agreed! +13
daviddoria 334 Posting Virtuoso Featured Poster

Can you give us an example piece of code as well as the output you are getting and the output you expect?

daviddoria 334 Posting Virtuoso Featured Poster

and please don't double post.

daviddoria 334 Posting Virtuoso Featured Poster

Please post a significant attempt and we can try to help if you get stuck. If you don't want to do your assignment, why should we??

Also, please use a much more descriptive thread title.

daviddoria 334 Posting Virtuoso Featured Poster

Please post a significant attempt and we can try to help if you get stuck.

daviddoria 334 Posting Virtuoso Featured Poster

Haha sfuo you beat me to the punch!

daviddoria 334 Posting Virtuoso Featured Poster

I don't know about this stdafx business, but from what my compiler tells me you need cstdio to use printf (but you should use std::cout instead). Also, strcat_s is not necessary, just use std::string and the '+' operator. I would even store the words originally in a std::vector<std::string>. I also corrected the spelling of 'sentence' :)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstdio>
using namespace std;


const char SIZE = 100;

int main(int argc, char* argv[])
{
    const char *article[5] = {"the", "a", "one", "some", "any"};
    const char *noun[5] = {"boy", "girl", "dog", "town", "truck"};
    const char *verb[5] = {"drove", "jumped", "ran", "walked", "flew"};
    const char *preposition[5] = {"to", "from", "over", "under", "on"};

  srand((unsigned)time(NULL));

  std::string sentence;
  for(unsigned int i = 0; i <= 20; i++)
  {
      sentence = std::string(article[rand()%5]) + " " +
                 std::string(noun[rand()%5]) + " " +
                 std::string(verb[rand()%5]) + " " +
                 std::string(preposition[rand()%5]) + " " +
                 std::string(article[rand()%5]);
      std::cout << sentence << std::endl;
  }
  
  return 0;
}

Hope that helps.

David

daviddoria 334 Posting Virtuoso Featured Poster

While that did work, this seems even better to me :)

Test.cxx

#include "MyDerivedClass.h"

int main(int, char *[])
{
  MyDerivedClass a;
  a.DoSomething();

  return 0;
}

MyClass.h

#ifndef myclass_h
#define myclass_h

namespace std
{
    template < typename T > struct allocator ;
    template < typename T, typename A > class vector ;
}

class MyClass
{
  public:
    MyClass();
    ~MyClass();

  protected:
    std::vector< double, std::allocator<double> >* MyVector;
};

#endif

MyClass.cxx

#include "MyClass.h"
#include <vector>

MyClass::MyClass()
{
  this->MyVector = new std::vector<double>;
}

MyClass::~MyClass()
{
  if(this->MyVector)
    {
    delete this->MyVector;
    }
}

MyDerivedClass.h

#ifndef myderivedclass_h
#define myderivedclass_h

#include "MyClass.h"

class MyDerivedClass : MyClass
{
    public:
        void DoSomething();
};
#endif

MyDerivedClass.cxx

#include "MyDerivedClass.h"
#include <vector>
#include <iostream>

void MyDerivedClass::DoSomething()
{
    this->MyVector->push_back(1.3);

    std::cout << (*this->MyVector)[0] << std::endl;
}

Thanks again!

daviddoria 334 Posting Virtuoso Featured Poster

Wow you're right, that does work! Thanks all!

daviddoria 334 Posting Virtuoso Featured Poster

Try

unsigned char temp[1]; // or however many characters you expect to get

Or better yet, why not use a std::string instead of a char array?

daviddoria 334 Posting Virtuoso Featured Poster

How are you declaring it? Give us some short, compilable code snippets to work with.

daviddoria 334 Posting Virtuoso Featured Poster

Ah, I actually saw that solution on StackOverflow - the problem was that in order to use std::allocator , you had to #include <memory> . If you are going to include 'memory', you might as well include 'vector', right?

David

daviddoria 334 Posting Virtuoso Featured Poster

If that is really the only way to do it, I'll have to try to fight the fight. However, they really like their structure (there are 2000 classes, so consistency is nice), so they would really like to see a single .h and a single .cxx for each set of functionalities (class).

daviddoria 334 Posting Virtuoso Featured Poster

Right, you have #included <vector> in the header. This is not allowed in the library I am working on.

daviddoria 334 Posting Virtuoso Featured Poster

@Mike - I can't add another header like that :(

@AncientDragon - That didn't work either - same errors:

MyClass.h

#ifndef myclass_h
#define myclass_h

class MyClass
{
  public:
    MyClass();
    ~MyClass();

  protected:
    struct MyVector;
    MyVector* vecPtr;
};

#endif

MyClass.cxx

#include <vector>
#include "MyClass.h"

struct MyClass::MyVector
{
  std::vector<double> v;
};

MyClass::MyClass()
{
  this->vecPtr = new MyVector;
}

MyClass::~MyClass()
{
  delete this->vecPtr;
}

MyDerivedClass.h

#ifndef myderivedclass_h
#define myderivedclass_h

#include "MyClass.h"

class MyDerivedClass : public MyClass
{
  public:
  void DoSomething();
};

#endif

MyDerivedClass.cxx

#include <vector>
#include "MyClass.h" //I even added this!
#include "MyDerivedClass.h"

void MyDerivedClass::DoSomething()
{
  this->vecPtr->v.push_back(1);
}
daviddoria 334 Posting Virtuoso Featured Poster

That is the whole problem - I am not allowed to #include <vector> in any header file.

daviddoria 334 Posting Virtuoso Featured Poster

Those are good ideas/suggestions, but it still doesn't work!

Here are the files that changed:

MyClass.h

#ifndef myclass_h
#define myclass_h

class MyClass
{
  public:
    MyClass();
    ~MyClass();

  protected:
    struct MyVector;
    MyVector* vecPtr;
};

#endif

MyClass.cxx

#include "MyClass.h"

#include <vector>

struct MyClass::MyVector
{
  std::vector<double> v;
};

MyClass::MyClass()
{
  this->vecPtr = new MyVector;
}

MyClass::~MyClass()
{
  delete this->vecPtr;
}

The errors are actually identical.

David

daviddoria 334 Posting Virtuoso Featured Poster

Hm I had tried that (and just tried it again) and it didn't help :(

daviddoria 334 Posting Virtuoso Featured Poster

I am trying to remove the need to #include stl elements in my header (a requirement for a project I work on). I wrote a "wrapper" for std::vector called "MyVector". I declare a pointer to a MyVector as a member of MyClass. Then when I try to use this in MyDerivedClass, I get

"invalid use of incomplete type 'struct MyVector'
forward declaration of 'struct MyVector'

Can anyone see what I have done wrong below?

Test.cxx

#include "MyDerivedClass.h"

int main(int, char *[])
{
  MyDerivedClass a;
  a.DoSomething();
  
  return 0;
}

MyClass.h

#ifndef myclass_h
#define myclass_h

struct MyVector; //forward declaration of 'struct MyVector'

class MyClass
{
  public:
    MyClass();
    ~MyClass();

    MyVector* vecPtr;
};

#endif

MyClass.cxx

#include "MyClass.h"
#include <vector>

struct MyVector
{
  std::vector<double> v;
};

MyClass::MyClass()
{
  this->vecPtr = new MyVector;
}

MyClass::~MyClass()
{
  delete this->vecPtr;
}

MyDerivedClass.h

#ifndef myderivedclass_h
#define myderivedclass_h

#include "MyClass.h"

class MyDerivedClass : MyClass
{
  public:
  void DoSomething();
};

#endif

MyDerivedClass.cxx

#include "MyDerivedClass.h"

void MyDerivedClass::DoSomething()
{
  this->vecPtr->v.push_back(1); //"invalid use of incomplete type 'struct MyVector'
}

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

It should be the same type as the elements of mpImg. How is mpImg declared?

daviddoria 334 Posting Virtuoso Featured Poster

First I would be really careful about calling a variable 'size'. It could definitely clash with other things.

Let me recommend that you add comments to the major portions of your code. This will not only help us read what is going on in your code, but I bet once you translate that code to English you will understand what is going wrong.

daviddoria 334 Posting Virtuoso Featured Poster

I would suggest making a demo program to make sure you understand how stringstream works before using it in your program with user input. If you hard code values and it works as you'd expect, then you know the problem is with the input itself.

daviddoria 334 Posting Virtuoso Featured Poster

A second vote for cmake!

daviddoria 334 Posting Virtuoso Featured Poster

Try looking up "function pointers". Does this example help? http://programmingexamples.net/index.php?title=CPP/FunctionPointer

David

daviddoria 334 Posting Virtuoso Featured Poster

My suggestion is to first create a demo program where there is no user input (hardcode or remove anything that you can to still reproduce the problem). This way we all know exactly what the state of the program is when looking at your code.

David

daviddoria 334 Posting Virtuoso Featured Poster

What language/database system is this? Please provide a compilable example of what you are trying to do and maybe someone will be able to get it to work for you.

David

daviddoria 334 Posting Virtuoso Featured Poster

What have you tried so far? If you post some code and tell us what is wrong with it we may be able to help.

David

daviddoria 334 Posting Virtuoso Featured Poster
daviddoria 334 Posting Virtuoso Featured Poster

Haha what I meant was not "mark as solved" on DaniWeb, but a "solved problem" in the research field (i.e. everyone agrees no further work is necessary).

daviddoria 334 Posting Virtuoso Featured Poster

My only suggestion is to not get frustrated - this is a very difficult problem and it is definitely not "solved". People still devise clever tricks tailored to their particular data sets, so you will certainly not be able to find any "one size fits all" algorithm to do something like this.

Good luck!

daviddoria 334 Posting Virtuoso Featured Poster

Const just means that the function is unable to modify the value. Of course if you don't want to change the value, then just don't change it! Const just provides a double check in case you made a mistake.

daviddoria 334 Posting Virtuoso Featured Poster

You will save yourself countless headaches by not using double arrays, but rather double vectors:

http://programmingexamples.net/index.php?title=CPP/2DVector

daviddoria 334 Posting Virtuoso Featured Poster

Is this a "clever" trick to get us to do your assignment? This should only be like 3 lines, right?

daviddoria 334 Posting Virtuoso Featured Poster

I think "code snippets" are intended to be "here is my working code, I thought it may help others". In your case, with a question, you should just make a normal "thread".

daviddoria 334 Posting Virtuoso Featured Poster

Why don't you just write another program to replace the "show desktop" program, and have your program do whatever it wants to and then call the real "show desktop"?