template<> 37 Junior Poster

similar to what you have, but max is not 2...

Also, personally, I would use a vector for input, less error prone...

#include <algorithm>
#include <ctime>

class MyRandom
{
public:
    ptrdiff_t operator() (ptrdiff_t max)
    {
        printf("max=%d-",(int)max);
        return rand()%max;
    }
};

int main () 
{
  srand ( unsigned ( time (NULL) ) );

  int numbers [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  int size = sizeof(numbers)/sizeof(int);

  MyRandom r;
  std::random_shuffle (numbers, numbers+size, r);

  return 0;
}
template<> 37 Junior Poster

use a printf to print max and show result

template<> 37 Junior Poster

ok, main calls random_shuffle,

where is the implementation of random_shuffle?

template<> 37 Junior Poster

max is most likely 2, because you must be passing 2

Code presented is incomplete, so its hard to figure out why.

template<> 37 Junior Poster

Depending on the input device used, most probably there is a start and end events generated. If so that would may be the cleanest solution to the problem.

template<> 37 Junior Poster

Perhaps you can maintain state when a point is hit and only update when it changes.

template<> 37 Junior Poster

Perhaps a custom sort on the vector might solve your problem. See attached link for ideas

http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects

template<> 37 Junior Poster

Most probably. Perhaps you only want to fire this code on the first mouse click (down or up) only.

template<> 37 Junior Poster

put a print statement before the for loop

printf("entering for loop...");

and see what happens...

template<> 37 Junior Poster

In that case its not clear what the problem is. Try to narrow it down and give us some sample code where the problem manifests itself.

template<> 37 Junior Poster

What are the types for k and playerA.score, int?

template<> 37 Junior Poster

Please provide example

template<> 37 Junior Poster
struct timeval { 
    unsigned long  tv_sec;   /* seconds since Jan. 1, 1970 */ 
    long           tv_usec;  /* and microseconds */ 
};
template<> 37 Junior Poster

try using gettimeofday, will give you usec rather than seconds

sample code http://www.daniweb.com/software-development/cpp/threads/361138

template<> 37 Junior Poster

1. Allocation means to create space for your application use
2. Initialize means to assign specific values to that space

Using malloc creates space, but contents of space undefined

template<> 37 Junior Poster

google c++ const

template<> 37 Junior Poster

I almost always disable the precompiled header, less problems in the long run.

template<> 37 Junior Poster
struct Point;                       // forward reference
struct MyClass1{Point &p;}; // reference to Point (ok)
struct MyClass2{Point *p;};  // pointer to Point (ok)
strcut MyClass3{Point  p;};  // Pointer Object (compile error)

Forward reference are used to inform the compiler a name is valid and defined somewhere else. However if the size of the object is required or any of the methods are used, then the actual definition is required.

No difference in runtime performance although using forward reference may reduce compile time in some cases since the header files is not required.

template<> 37 Junior Poster

Have you tried google? eight queen problem c++ recursion

Lots of information on the classic problem, below are some..

http://wn.com/eight_queens_problem

http://en.wikipedia.org/wiki/Eight_queens_puzzle

http://people.moreheadstate.edu/fs/d.chatham/dlxMCCC.pdf

template<> 37 Junior Poster

Without having code that compiles...

The signature may be incorrect. void insertNode(T);

Seems like you want to insert a node, not a type, which is represented by T?

template<> 37 Junior Poster

Interesting problem. Below is a small sample of the problem and fix. Seems like you need to Llist<T>::. http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

template <class T>
class Llist
{
public:
    Llist(){;}
    void remove_head()
    {
    }
private:
    T foo;
};

template <class T>
class Stack :  public Llist<T>
{
public:
    Stack(){}
    void pop()
    {
         //remove_head();
        Llist<T>::remove_head();
    }
};

int main()
{
    return 1;
}
template<> 37 Junior Poster

Where is Node defined?

template<> 37 Junior Poster

Quote: "However, I am developing a collection of classes and I need them all to share a common variable (a time step) whose value will be defined by the user."

Since the values is defined by user, its similar to a configuration value, therefore const is probably not what you want.

A singleton that the user can initialize at startup maybe another alternative

template<> 37 Junior Poster
template<> 37 Junior Poster

A structure is a class where members are public by default, besides that technically speaking there are same. However by convention, structures are used to represent simple data structures and classes for more expressive functionality.

For this simple application, it’s not a big deal, but meticulous attention to Constructors and Destructors, including the initialization and order of initialization will save you hours of debugging time in the future.

template<> 37 Junior Poster

Some questions to ask yourself.
1. class or structure?
2. Inside the class, you do not need this-> to reference data members
3. Initialize in constructor rather than assign

For example

struct Node
{
    int data;
    Node *preNode;
    Node *postNode;
    Node *parentNode;
    bool isEmpty;

    Node()
    : data(0)
    , preNode(NULL)
    , postNode(NULL)
    , parentNode(NULL)
    , isEmpty(true)
    {
    } 
};
template<> 37 Junior Poster

if isNull means all nodes are null, then an alternative is to have method to check nodes are null

template<> 37 Junior Poster

The pointers to nodes initialized to NULL make sense.

But what/how is the isNull used for?

template<> 37 Junior Poster

Let see some code for what your trying to do

template<> 37 Junior Poster

In c/c++ its a good idea to initialize variables to some known state. Unintialized variables causing problem has kept many a programmer debugging for hours.

In C++ Null is defined as 0. See section on "Should I use NULL or 0?" for some words of wisdom from the master himself.

http://www2.research.att.com/~bs/bs_faq2.html

iamcreasy commented: Awesome Link :D +2
template<> 37 Junior Poster

code is simple should work either way

template<> 37 Junior Poster

use the CODE blocks to demark your code, so its simpler to read

class Hello
{

};
template<> 37 Junior Poster

g++ -Wall main.cpp myclass.cpp friendclass.cpp -o friend

#ifndef MYCLASS_H
#define MYCLASS_H

#include "friendclass.h"

class MyClass
{
    public:
        MyClass();
        int GetprivVar1() { return privVar1; }
        //friend class FriendClass;
        friend void FriendClass::changeMyVariable(MyClass*);
    protected:
    private:
        int privVar1;
};

#endif // MYCLASS_H
/****************** FriendClass.h ***************/

#ifndef FRIENDCLASS_H
#define FRIENDCLASS_H

class MyClass;

class FriendClass
{
    public:
        FriendClass();
        void changeMyVariable (MyClass *);
    protected:
    private:
};

#endif // FRIENDCLASS_H
/****************** MyClass.cpp ***************/

#include "myclass.h"

MyClass::MyClass()
{
    privVar1 = 0;
}
#include "friendclass.h"
#include "myclass.h"

FriendClass::FriendClass()
{
    //ctor
}

void FriendClass::changeMyVariable(MyClass* obj)
{
    obj->privVar1 = 9;
}
#include <iostream>
#include "friendclass.h"
#include "myclass.h"

using namespace std;
int main()
{
    MyClass obj1;
    FriendClass obj2;
    cout<<"before : "<<obj1.GetprivVar1();
    obj2.changeMyVariable(&obj1);
    cout<<"after : "<<obj1.GetprivVar1();

    return 0;
}
template<> 37 Junior Poster

Make sure the cpp include the header files of what is needed

template<> 37 Junior Poster

I am able to compile and link using g++, with changes below.

1. in FriendClass.h remove MyClass.h include and provide forward declaration for MyClass

2. In MyClass.h include FriendClass.h

template<> 37 Junior Poster

You could create one thread as a service thread that calls the public methods of your objects every minute...

template<> 37 Junior Poster

from within the q1_sort function, when it calls itself,

it needs to pass the typename like q1_sort<Tdata>(A, left, i-1);

(your original code has person type hardcoded in the function)

that should fix your undefined reference

template<> 37 Junior Poster
template <typename Tdata>

void q1_sort(vector<Tdata> &A, int left, int right)
{
    if((right-left) < ISORT_CUTOFF){ 
        if(data_t == 1)
        {in_sort<Tdata>(A, (int)left, (int)right+1);}   
        else if(data_t == 2)
        {in_sort<Tdata>(A, (int)left, (int)right+1);} 
    }
    else{
    q1_sort<Tdata>(A, left, i-1);
    q1_sort<Tdata>(A, i+1, right);
    }
}
Koinutron commented: Good, quick solution to a problem that was driving me nuts! and I learned good info about template use +3
template<> 37 Junior Poster

This can you you more information than I can:
http://www.cplusplus.com/reference/iostream/istream/

Basically, error checking is a big part of writing programs. If your reading ch by ch, you need to ensure stream is still valid. Stuff can go bad a zillion different ways... at the worst possible time!!!

template<> 37 Junior Poster

pseudorandom21 solution is clean and simple, spend some time to understand it

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::stringstream strm("it your move");

    std::string token;
    while(strm >> token)
    {
        std::cout << token << std::endl;
    }

    return 0;
}
template<> 37 Junior Poster

In your version you are mixing types, not sure what that will do. For example you are assigning bool to char and comparing char to bool, might want to fix that.

char character;
character=false;

character == false

template<> 37 Junior Poster

An alternate way that might work for you as suggested above...

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;
int main()
{
    ifstream istrm;

    istrm.open("proj-8.dat");

    char ch;

    while( istrm.get(ch) )
    {
        if( ch == ' ' )
        {
            cout << ch;
            // strip extra space
            while( istrm.get(ch) && ch == ' ' )
                ;
        }

        if( istrm.good() )
            cout << ch;
    }

    istrm.close();
}
template<> 37 Junior Poster

Might be cleaner if you design you problem in terms of objects (use classes)

template<> 37 Junior Poster

Show the actual error with line number...

template<> 37 Junior Poster

When you hit a space, write it out
then find all sebsequent spaces and throw them away.

WaltP commented: You're kidding. Really? How'd you come up with that? :icon_rolleyes: -3
template<> 37 Junior Poster

GetTickCount() might give you what you want.

http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx

template<> 37 Junior Poster

You might want to consider to first break the problem into set of points (x,y). Then extract and convert each set of points to numbers.

template<> 37 Junior Poster

I am able to take the code out of above and compile without any problem.

g++ -g sample.cpp -o sample

Are you able to do that?

template<> 37 Junior Poster

typedef is handy when using stl containers to avoid typing. Read up on it. Below is a simple sample to help to compile, run, test and understand. Play around with it and when you understand whats going on, incorporate similar logic into your application.

Iterators are tricky and confusing at first, but its extensively used, so its worthwhile investment in time.

#include <iostream>
#include <vector>

int main( int argc, char *argv[])
{
    /// typedef lets us alias names
    typedef std::vector<std::string> TVec;

    /// below is a list of favarite dogs
    TVec v;
    v.push_back("jake");
    v.push_back("lucy");
    v.push_back("paws");

    std::cout << "Please enter name to remove:" << std::endl;
    std::string who;
    std::getline(std::cin,who);
 
    /// find and remove name on list
    bool found=false;
    for( TVec::iterator iter=v.begin(); iter!=v.end() && found==false; ++iter )
    {
        if( *iter == who )
        {
            v.erase(iter); // iterator passed to erase
            found=true; // exit the loop
        }
    }

    /// inform user of status
    if(found == true )
    {
        std::cout << who << " is removed" << std::endl;
    }
    else
    {
        std::cout << who << " is not on list" << std::endl;
    }

    return 0;
}
template<> 37 Junior Poster

If you must use clock then you problably do'nt want to use difftime since difftime is expecting time_t as input rather than clock_t. Note the function signature. double difftime ( time_t time2, time_t time1 );

Sample code show what happens when you pass clock_t into difftime rather than using time_t. You get a much larger delay.

1 #include <iostream>
  2 #include <time.h>
  3 
  4 
  5 int main()
  6 {
  7     clock_t c1 = clock();
  8     time_t t1 = time(0);
  9     
 10     sleep(1); 
 11     
 12     time_t t2 = time(0);
 13     clock_t c2 = clock();
 14     
 15     size_t cdiff = difftime(c2,c1);
 16     size_t tdiff = difftime(t2,t1);
 17     
 18     std::cout << "clock diff=[" << cdiff << "]" << std::endl;
 19     std::cout << "time diff=[" << tdiff << "]" << std::endl;
 20     
 21     return 0;
 22 }