Solved

C++ "FIND_IF" & VECTORS

Posted on 2010-09-02
7
506 Views
Last Modified: 2012-05-10
I have the following snippet below for a vector class. It's called "DDClass" Mainly, since it will handle my damage done on ships.

I get the following error:
2>..\sf\frontier\src\portcpp\DesignedClass.cpp(1669): error C3867: 'DesignedClass::FindCondition': function call missing argument list; use '&DesignedClass::FindCondition' to create a pointer to member


in my designedclass.h file:

      Vector<DDClass, short> ddamage;
      template <int T> bool FindCondition(const DDClass& damdone) { return damdone.shipid == T; }


and in designedclass.cpp file:

      if (Type() == ship) {
            if (find_if(ddamage.begin(), ddamage.end(), FindCondition<15>))
                  g_logFile << "FOUND" << endl;
            else {
                  g_logFile << "NOT FOUND" << endl;
            }
      }

And the main DDClass.h that hanldes the vector class (that is attached)
DDClass.h

#ifndef DDCLASS_H
#define DDCLASS_H

class DDClass {
public:
    int shipid;
    long damdone;
      long cframe;
      DDClass(){}
      DDClass(int sid, long dd, long f)
            : shipid(sid), damdone(dd), cframe(f){}
      void print()const{
            cout << shipid << ", " << damdone << " on frame: " << cframe << "\n";
      }
};

#endif

'15' is the shipid I want it to find, as a test. Mainly I think this is becuase of my custom vector class, but I am not sure how I can solve it/don't know what it wants me to exactly call. Does anyone have any clues? Mainly I want to search, and if not found, add it.

Thanks!
#include <vector>

#include <ostream>

using namespace std;



template<class T, class I> class Vector {

private:      

	std::vector<T> vec;

	std::ostream* err;

public:

	  Vector(void) {

            err = &g_logFile;

      }



      Vector(I i, ostream* o =&g_logFile) 

                     : vec(i, T()), err(o)

                {

      }



      ~Vector(){

            vec.clear();

      }



      int initializeWith(I i, ostream* o =&g_logFile) {

            vec.clear();

            vec.resize(i, T());

            err = o;

            return i == vec.size()? 0 : -1;

      }



      void _initialize() {

            vec.clear();

            err = &g_logFile;

      }



      void initialize() {

            vec.clear();

            err = &g_logFile;

      }



      void destroy(){

            vec.clear();

			vec.swap( vec );

	  }



	  Vector<T,I>& operator=(const Vector<T,I>& a) {

            if (this != &a) {

                  vec = a.vec;

                  err = a.err;

            }

            return *this;

      }



	void extendBy(I i) 

	{

		vec.resize(vec.size()+i, T()); //No return with resize.

	}



	void add(T& t) {

		extendBy(1);

		vec[vec.size()-1] = t;

	}



	T* begin() {

		return vec.begin();

	}



	T* end() {

		return vec.end();

	}



	inline I size() const{

		return vec.size();

	}



etc..

Open in new window

0
Comment
Question by:Valleriani
  • 5
  • 2
7 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 33588283
Try changing this...

 if (find_if(ddamage.begin(), ddamage.end(), FindCondition<15>))


to this...

 if (find_if(ddamage.begin(), ddamage.end(), &DesignedClass::FindCondition<15>))
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33588298
BTW: unless DesignedClass::FindCondition is static member you can't use it as a predicate for find_if
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 33588348
Something else, having a using namespace in a header is a problem waiting to happen. Every translation unit that includes that header will be polluted with namespace you use. You should avoid doing this.


The types your begin and end return are wrong, they should be iterators and not pointers.

Honestly, I would avoid having this vector adaptor class... it's just a layer of abstraction that is likely to become a maintenance overhead.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Author Comment

by:Valleriani
ID: 33588725
Problem is unfort the game used to use a array.h class, which was a easy swap  to vector.h without messing with alot right now. There is 'alot' of coding to change, even though it would be best, mainly there hasn't been issues (crashes/etc) using it. I actually just added the begin/end recently, but I misjudged it by mistake, and thanks for pointing that out!

Though I'd like to eventually fix it properly, but this is one of those things I don't like to play with just yet because I haven't played with them before.

And thank you for the header tip. I'm going to go check it out now and I'll let you know.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 500 total points
ID: 33588784
>> I actually just added the begin/end recently, but I misjudged it by mistake, and thanks for pointing that out!

If you need to maintain pointer semantics you could do this...
       T* begin() {

                return &vec[0];

        }



        T* end() {

                return &vec[vec.size()];

        }

Open in new window

0
 
LVL 7

Author Closing Comment

by:Valleriani
ID: 33589083
Removing it from the header and the begin/end fix seemed to work well. Tested and working, thanks!
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33589102
Welcome :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 74
VB6 Compile Compatibility Issue 4 91
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 97
Turning python script into an applet 12 106
A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now