| | |
6 Line class -> Me pounding head into wall
![]() |
•
•
Join Date: Sep 2004
Posts: 56
Reputation:
Solved Threads: 2
I need to write a class using a vector and this is my first time using vectors. But I have looked through several books/web pages and can't see why this thing won't compile.
#include <vector>
template <class el>
class pQ
{
public:
private:
vector<el> a(1);
};
Any ideas. There must be some small syntax issue here I cant see. The error is something along the lines of using a struct that is not defined.
#include <vector>
template <class el>
class pQ
{
public:
private:
vector<el> a(1);
};
Any ideas. There must be some small syntax issue here I cant see. The error is something along the lines of using a struct that is not defined.
Your problems all stem from this line:
First and foremost you need to remember that any standard names are in the std namespace. If you don't have a using declaration (using std::vector;) or a using directive (using namespace std;) then you need to qualify the declaration explicitly:
The second problem is that you're trying to call a constructor in a class declaration. Because this isn't executable code, you want to remove the argument list:
Then you can call the constructor of a in the constructor of pQ:
Though because vectors grow dynamically, you typically don't need to worry about this and can just rely on push_back working it's magic. Only in extreme cases of performance or if the vector is not likely to change its size do you want to set a base size or preallocate a certain amount of memory.
C Syntax (Toggle Plain Text)
vector<el> a(1);
C Syntax (Toggle Plain Text)
std::vector<el> a(1);
C Syntax (Toggle Plain Text)
std::vector<el> a;
C Syntax (Toggle Plain Text)
template <class el> class pQ { public: pQ() : a ( 1 ) {} public: std::vector<el> a; };
I'm here to prove you wrong.
•
•
Join Date: Sep 2004
Posts: 56
Reputation:
Solved Threads: 2
C Syntax (Toggle Plain Text)
template <class el> class pQ { public: pQ() : a ( 1 ) {} public: std::vector<el> a; };
Thanks, that helped some. I am having a few problems though. I couldn't use that exact code snippet due to certain ways the code needs to be formatted. Here is the relevant code. This class is being called as an int.
using namespace std;
template <class el>
class pQ
{
public:
pQ();
private:
vector<el> a;
};
template <class el>
pQ<el>:: pQ()
{
a(1);
}
Is it possible to do it this way? The error is:
no match for call to '(std::vector <int, std:allocator <int> >) (int)
Thanks again.
>Is it possible to do it this way?
No, what I showed you was an initializer list for the class constructor. This list has special properties that allow you to actually initialize variables rather than just assign to them as you would have to in the constructor body. If you do not use the initializer list then the vector will be default constructed and you need to use resize:
No, what I showed you was an initializer list for the class constructor. This list has special properties that allow you to actually initialize variables rather than just assign to them as you would have to in the constructor body. If you do not use the initializer list then the vector will be default constructed and you need to use resize:
C Syntax (Toggle Plain Text)
template <class el> class pQ { public: pQ(); private: vector<el> a; }; template <class el> pQ<el>:: pQ() { a.resize ( 1 ); }
I'm here to prove you wrong.
![]() |
Similar Threads
- OOPing with PHP4 simple DB connect class? (PHP)
- Linked Lists in multiple classes (C++)
- How to populate an array? from a FILE (C)
- This ought to be simple - extra spaces (PHP)
Other Threads in the C Forum
- Previous Thread: How to change a button's shape ?
- Next Thread: Quicksort/Insertion sort Hyrbid?
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






