I'm quite new to the standard template library, I'm trying to understand how to use them with my code. There may still be some logic errors in my code. However I am trying to sort out my syntax errors.

One big one is this:

type/value mismatch at argument 1 in template parameter list for template<class _Tp, class _Sequence> class std::queue'
expected a type, 'got T' //that too me is hilarious how its worded.

I know once I get this figured out I'll probably have alot more questions about whats going on, so please stick with me, and thanks.

#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

template <int T>
class BruteForce {
	private:
	int currentItem, nextItem;
	std::queue<T> q;
	public:
	void MakeQueue();
	void GetPercepts();
	int reflexAgent (int,int);
	void DisplayInfo();	
};
void BruteForce::MakeQueue() {
  ifstream myfile;
  myfile.open("sampleInput_reflex.txt");
  while (!myfile.eof())
  {
   int i;
   myfile >> i; 
   q.Push (i);
  }
  myfile.close(); 
  }

void BruteForce::GetPercepts() {
   if (q.size() > 1)
  {
   currentItem = q.front();
   q.pop();
   nextItem = q.front();
   q.pop(); 
  }
  else if (q.size() > 0)
  {
   currentItem = q.front();
   q.pop();
  }
  else 
  {
  //the queue is empty.
  } 


}

int BruteForce::reflexAgent (int x, int y) {

int stop = 0;
int pickup = 1;
int advance = 2;
 
if (x || y == -1)
{return stop};

else if (x > y)
{return pickup};

else if (x > y)
{return advance};

}

void BruteForce::DisplayInfo() {

If (results == 0)
{
cout << "currentItem: " << currentItem << "\n"
      << "nextItem: " << nextItem << "\n"
      << "Action: Stop" << "\n\n";
}
else if (results == 1)
{
cout << "currentItem: " << currentItem << "\n"
      << "nextItem: " << nextItem << "\n"
      << "Action: Pickup" << "\n\n";
}
else if (results == 2)
{
cout << "currentItem: " << currentItem << "\n"
      << "nextItem: " << nextItem << "\n"
      << "Action: Advance" << "\n\n";
}
} //end displayInfo
Main()
{
//this looks something like this:

BruteForce <int> robot;
//I then call all functions using the robot object.
return 0;

Recommended Answers

All 3 Replies

If you see a class template declared as so:

template <class SomeType>
class Foo {
  //...
};

It means that to use this class, you need to provide another class (or type in general) as argument to the template. The STL queue type is similar (except that it has a few more template arguments that have default values). This means you need to put some sort of type name as template argument, as in the following, for a queue of integers:

std::queue<int> myQueue;

From the looks of your code, it seems that really what you want to have is just like in the above, that is, a queue of integers. So, your class should be declared as:

//#include <iostream>
//#include <fstream> //don't include unnecessary headers (include them in the cpp instead.
#include <queue>
//using namespace std; //don't import namespaces in a header file.

//template <int T> //T seems to be useless and BruteForce does not need to be a class template.
class BruteForce {
  private:
    int currentItem, nextItem;
    std::queue<int> q;
  public:
    void MakeQueue();
    void GetPercepts();
    int reflexAgent (int,int);
    void DisplayInfo();	
};

The reason you had the error that you had is because you declared BruteForce as a class template with an integer-valued argument. Because template arguments can be either a type name (or a variation of that) or a constant value (known at compile-time) of some primitive type (int, float, double, char, etc... but not pointers or string types). This is actually an extremely powerful feature, but I will leave that aside. Since you have used the form "int T" for your template argument, it is essentially the same as a parameter syntax in a function (except that T has to be resolvable at compile-time). This means that T will take the value of whatever literal or compile-time constant value is used to instantiate the class template BruteForce. In this case, the correct instantiation of the class BruteForce would have been:

BruteForce<42> robot; //here 42 is a placeholder for whatever literal integer value.

With the above, it would leave only the error that the STL queue type takes as first argument a type name, but writing "queue<T>" actually gives a compile-time constant integer value as the first template argument, which is incorrect since it should be a type name like int or float or myClass, and it throws the error that you have reported.

commented: Thankyou! Your feedback helped me a great deal. +1

I may have mis-understood, but you could declare your class as

template <typename T>    // Replacing template< int T >
class BruteForce {
        private:
        int currentItem, nextItem;
        std::queue<T> q;
        public:
        void MakeQueue();
        void GetPercepts();
        int reflexAgent (int,int);
        void DisplayInfo();
};

This should allow to to create a BruteForce object that handles various types, not just int . If you just want int then you don't need a template class, as the previous poster said.

Also, I think you've made a logic error (probably due to copy & paste) in your function BruteForce::reflexAgent() , the two else if() statements have the same condition, the last one will never be executed.

I'm having a minor issue with the queue now. Thanks for all the advice earlier it helped a great deal!

main ()
{
BruteForce robot;

while (!robot.q.empty()) //is this a valid working condition?? could be issue w/ getPercepts()
{
/* call all functions in the BruteForce Class */
return 0;
}

///with the output everything works fine except it displays stop twice?? I'm not sure why, please help me with this. Same functions as above. I believe the error is that there is something strange going on with the condion of stopping in a while loop somewhere. I'm aware of the < > issue, it is fixed in real code :)
Thanks so far! Great Help!!

//////here is the output:
with input example: 78459-1

currentItem: 78
nextItem: 45
Action: Pickup

current: 9
nextItem: -1
Action: Stop

currentItem: -1
nextItem: -1
Action: Stop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.