0

It's been awhile since I've done C++ so I'm having a bit of trouble here. I'm getting this error on the line where I declare allQueue in the main file. I've obviously stripped out a lot of code that I don't think is required, if you need anything more let me know.

Compiling with

g++ mainFile.cpp MyClass.cpp extraObjectFile.o -o mainFile

Generates:

error: expected constructor, destructor, or type conversion before ‘<’ token

main file

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>    
#include "MyClass.h"

vector<MyClass> allQueue;
int main()
{
   allQueue.push_back(new MyClass(100));
}

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass(int start_priority)
{
    priority = start_priority;
}


int MyClass::getPriority()
{
    return priority;
}

MyClass.h

class MyClass
{
    int priority;
    public:
        MyClass(int);
        int getPriority();
};
2
  • Since nobody explains why to add std:: before vector, and since its not worthy of its own answer, it is because the vector template belongs to the std namespace. An alternative would be to have using namespace std; after your includes. See http://www.cplusplus.com/doc/tutorial/namespaces/ Commented Feb 21, 2012 at 21:05
  • right. I copied some code from an old cpp file I wrote a couple years ago and I was comparing this file to my old one and I must have forgot to copy the namespace line. never would have found it... Commented Feb 21, 2012 at 21:10

3 Answers 3

6

Ok, first thing to note: When you post an error message, please post the full error message your compiler gave you. Otherwise, nobody might be able to help you.

Secondly, the answer to your question:

Use

std::vector<MyClass> allQueue;

in your main.cpp file. And do a

allQueue.push_back(MyClass(100));

to add objects. Consider implementing a copy constructor for MyClass also.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 this took away all compiler errors. Is it considered better practice to use vector<MyClass> or vector<MyClass*> ?
@jb It depends on what you want to store. One stores a pointer, the other the object itself. For most cases you want to simply store the object. Keep in mind when storing pointers you still have to delete them when erasing from the vector or else you get a memory leak.
@jb. Whether you use a MyClass or a MyClass* as the container element for the vector depends entirely on what you want to achieve. Both approaches have their pro's and con's. However, a lot of people would argue that using MyClass is safer, and only if you really have to use pointers, you should consider using a smart pointer (such as boost::shared_ptr from Boost or std::shared_ptr if you already have C++ 11 support).
1

Change vector to std::vector.

Comments

1
  1. Use std::vector, not simply vector.

  2. Operator new returns a pointer to MyClass, not an object itself. You need make a vector of pointers (vector<MyClass*>) or you need to use MyClass() constructor like this

    MyClass m(100); allQueue.push_back(m);

or simply that

allQueue.push_back(mMyClass(100));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.