I've got header file with template class:
#ifndef BUBBLE_H
#define BUBBLE_H
#include "algorithm.h"
template <typename T>
class Bubble : public Algorithm <T> {
public:
Bubble(T* in, int inSize) : Algorithm<T>(in, inSize){}
void compute();
};
#endif // BUBBLE_H
if I put whole body of compute() class here everything works fine. But I would like to have it in cpp file. I wrote:
#include "bubbleSort.h" using namespace std;
template <typename T>
void BubbleSort<T>::compute(){ //(*)
for (int i = 1; i<this->dataSize; i++){
for (int j = this->dataSize-1; j>=i; j--){
if(this->data[j] < this->data[j-1]) swap(this->data[j-1], this->data[j]);
}
} }
But I received error in line (*):
error: expected initializer before '<' token void
BubbleSort::compute(){
^
How I should fix it?
It's because you're mixing up Bubble and BubbleSort, probably also the headers bubble.h and bubbleSort.h.
Related
Initialization of a static variable at the header file by using the templates (Used c++11, the 'inline' approach not supported)
cat base_class.hpp
#include <string>
#ifndef PROGRAM_BASE
#define PROGRAM_BASE
template <typename T>
struct S
{
static std::string s_elfName;
};
template <typename T>
std::string S<T>::s_elfName; //static initialization
class program_base : public S<void>{
public:
static std::string GetElfName() { return S<std::string>::s_elfName; }
bool update_string();
};
#endif
cat base_class.cpp
#include "base_class.hpp"
bool program_base::update_string(){
S<std::string>::s_elfName = "UpdateString";
}
cat read_string.cpp
#include <iostream>
#include "base_class.hpp"
using namespace std;
int main () {
program_base pb; pb.update_string();
cout << program_base::GetElfName() << endl;
}
The above one works fine, When I try to add templet inside the "class program_base"
cat base_class.hpp
#include <string>
#ifndef PROGRAM_BASE
#define PROGRAM_BASE
class program_base {
public:
template <typename T>
struct S
{
static std::string s_elfName;
};
template <typename T>
std::string S<T>::s_elfName; //static initialization
static std::string GetElfName() { return S<std::string>::s_elfName; }
bool update_string();
};
#endif
it's giving an error as " error: member 's_elfName' declared as a template"
Why I'm not able to declare a template inside the class instead of inheriting it?
As stated in the C++ Std. Nested class declaration:
Member functions and static data members of a nested class can be defined in a namespace scope enclosingthe definition of their class.
[Example:
struct enclose {
struct inner {
static int x;
void f(int i);
};
};
int enclose::inner::x = 1;
void enclose::inner::f(int i) { /* ... */ }
— end example]
If you take the definition out of outer class then it should be fine:
template <typename T>
std::string program_base::S<T>::s_elfName;
Since C++17 you can also do inline static initializing:
class program_base {
public:
template <typename T>
struct S
{
inline static std::string s_elfName;
};
static std::string GetElfName() { return S<std::string>::s_elfName; }
bool update_string();
};
So this is for my data structures class and I am struggling to understand why I am getting the error: invalid pointer: 0x00000000023ce048 ***. It only happens when I define my generic class using strings: NSequence<string> v3(10);
It should be noted that using int or even a different class worked fine. However, as soon as I write the line: NSequence<string> v3(10); I get the pointer error and I have no idea why?
UPDATE:
I've found that it was actually my destructor class that was causing the error. However, I now have no idea why this would cause an error specifically with string?
template<typename T>
NSequence<T>::~NSequence()
{
delete items;
}
Here is the definition of my class:
#include <algorithm>
#include <iostream>
#include <utility>
/* XXX: Implement all member functions for NSequence in NSequence.hpp */
template <typename T>
class NSequence
{
public:
explicit NSequence( int initSize = 0 );
private:
int numOfItems;
int totalCapacity;
T * items;
};
#include "NSequence.hpp" // do not change this line
#endif
Here is my constructor function:
template<typename T>
NSequence<T>::NSequence(int initSize)
{
if(initSize==0)
initSize=1;
numOfItems = initSize;
totalCapacity = initSize;
items = new T[totalCapacity];
}
Any advice would be greatly appreciated!
I am getting the error
declaration is incompatible with "void spectrogram<T>::update(<error-type> x)
I don't see any difference between the declaration and the definition of the method, not sure why it is complaining about just this one definition and not the constructor or destructor.
Here is vComplex.hpp
#ifndef VCOMPLEX_H
#define VCOMPLEX_H
template <class T>
class vComplex {
public:
T* realp;
T* imagp;
int length; // for bookkeeping
vComplex(void) { }
vComplex (T* I, T* Q, int len) {
realp = I;
imagp = Q;
length = len;
}
~vComplex(void) {
free(realp);
free(imagp);
}
void put(T* I, T*Q, int len) {
realp = I;
imagp = Q;
length = len;
}
};
#endif
the function declaration for update in spectrogram.hpp, with other members removed:
#ifndef SPECTROGRAM_H
#define SPECTROGRAM_H
template <typename T>
class spectrogram {
public:
void update(vComplex<T> x);
};
#endif
and the function signature (and includes) for update in spectrogram.cpp:
#include <stdio.h>
#include <math.h>
#include "spectrogram.hpp"
#include "vComplex.hpp"
template <typename T>
void spectrogram<T>::update(vComplex<T> x) {
//do stuff
}
In VS 2017, I get the red underline under update and everything inside of it breaks basically. VS is saying T is undefined which I'm assuming is caused by the overall error. I have to use dynamically allocated pointers, I don't have the option of using other types or containers.
in "dualstk.h"
#ifndef __32_dualstk_h
#define __32_dualstk_h
#include <deque>
#include <cstdlib>
#include <iostream>
using namespace std;
enum stackNumber {One, Two};
template <class T>
class DualStack{
public:
DualStack() {count1 = 0; count2 = 0;};
//constructor. set counts to 0
void push(const T& item, stackNumber n);
void pop(stackNumber n);
T& top(stackNumber n);
const T& top(stackNumber n) const;
bool empty(stackNumber n) const;
int size(stackNumber n) const;
private:
deque<T> dualstackElements;
int count1, count2;
};
//error here
void DualStack::pop(stackNumber n){
}
#endif
Any idea why I'm pulling this error? What's strangest is that this is the book "Data Structures with C++ using STL" code and it states that this part is supposed to be correct as we are to simply implement the functions.
When I go to implement a most basic function, I get the error: "Expected a class or namespace."
DualStack is a template so you need to use template syntax on your function implementation.
template <class T>
void DualStack<T>::pop(stackNumber n){
}
create a separate file for your function implementation and include your header file
#include "dualstk.h"
void DualStack::pop(stackNumber n){
}
I want to create a private function in C++ (VS2010).
It should return a vector/array of structs/userdefined type.
However I think my declaration of the function in the cpp file is perhaps wrong.
Or maybe already in the header. Can somebody have a look at it?
My header looks like this:
#pragma once
using namespace std;
#include <algorithm>
#include <vector>
class clsWString2
{
private:
struct udtWChar2
{
wstring Text;
int OrigPos;
};
bool m_bDirty;
vector<udtWChar2>pToWChar2(wstring u);
vector<udtWChar2>m;
public:
clsWString2(void);
~clsWString2(void);
void ReplaceCompareBinary(wstring uSearchFor, wstring uReplaceBy);
void ReplaceCompareText(wstring uSearchFor,wstring uReplaceBy);
void ReplaceByPos(int uStartPos1Based,int uLen0Based, wstring uReplaceBy);
void FeedString(wstring u);
void Append(wstring u);
wstring CharAtPos(int uIndex);
int OrigPos(int uIndex);
};
And my .cpp file looks like this:
#include "StdAfx.h"
#include "clsWString2.h"
clsWString2::clsWString2(void)
{
m.resize(0);
}
clsWString2::~clsWString2(void)
{
}
vector<udtWChar2> clsWString2::pToWChar2(wstring u)
{
vector<udtWChar2> n;
n.resize(0);
for (int i=0;i<u.size();i++)
{
wstring sChar;
sChar=u.substr(i,1);
udtWChar2 nc;
nc.Text =sChar;
nc.OrigPos=i;
n.push_back (nc);
}
return n;
}
In the source file, when you define the function, the return type is not in the scope of the class so the class in the vector needs to be fully qualified:
vector<clsWString2::udtWChar2> clsWString2::pToWChar2(wstring u)
{
...
}
Ah, I got it:
vector<clsWString2::udtWChar2> clsWString2::pToWChar2(wstring u)
You can not use directly udtWChar2 as type , you need define type def or you need use as struct udtWChar2
Like :
in .h
vector < struct udtWChar2 > pToWChar2(wstring u);
vector < struct udtWChar2 > m;
in .cpp
vector < struct clsWString2::udtWChar2 > clsWString2::pToWChar2(wstring u)