Error: some class is not a template - c++

I have a header file Algo.h. It has the following content:
#include <iostream>
#include <fstream>
#include <math.h>
#include <float.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
//some static functions
// ...
template <class Type> class Algo{
int
public:
Algo(int size, int num, int plth, int theN, float** theAg, int theLN,
float* theIn, float theeEps = 1E-3, float theEpsilonLR = 1E-3,
int theCycle = 30, bool DebInf = false, int theT = -1, int** theX = 0,
const char* theFileName = 0, const char* theFileNameChar = 0);
~Algo();
//some methods
//...
};
//Constructor
template <class Type> Algo<Type>::Algo(int size, int num, int plth, int theN, float** theAg, int theLN,
float* theIn, float theeEps = 1E-3, float theEpsilonLR = 1E-3,
int theCycle = 30, bool DebInf = false, int theT = -1, int** theX = 0,
const char* theFileName = 0, const char* theFileNameChar = 0){
//...
}
// ...
Then I'd like to usel it in main.cpp:
#include "Algo.h"
#include <float.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
Algo<int>* construct1(const int & rt, float** & rm); //error: Algo is not a template
Algo<int>* construct2(const int & rte, float** & rm, Algo<int>* & the1, const bool & rob1); //error: Algo is not a template
//...
int main(){
//...
return 0;
}
It seems that everything should work fine, but I always get this error:
Algo is not a template.
Do you have any ideas how to fix it?

there is "int" which should not be there in your code. delete it, please.
template <class Type> class Algo{
int // here should be deleted
public:
...
constructor of Algo has many default params, but when you define this function, these default params should not be set value in param-list. you can make the constructor definition as follows:
template <class Type> Algo<Type>::Algo(int size, int num, int plth, int theN, float** theAg, int theLN, float* theIn, float theeEps, float theEpsilonLR, int theCycle, bool DebInf, int theT, int** theX, const char* theFileName, const char* theFileNameChar)
{
//...
}
do these 2 fixs, it will works.( I have try it on my computer~ )

I don't think this is the only problem, but pay attention to how you attempt to use it.
The constructor:
Algo(int size, int num, int plth, int theN, float** theAg, int theLN,
float* theIn, float theeEps = 1E-3, float theEpsilonLR = 1E-3,
int theCycle = 30, bool DebInf = false, int theT = -1, int** theX = 0,
const char* theFileName = 0, const char* theFileNameChar = 0);
Has many parameters. The first 7 are required, the remainder have default values and are optional. However, when you try and instantiate an instance:
Algo<int>* construct1(const int & rt, float** & rm); //error: Algo is not a template
Algo<int>* construct2(const int & rte, float** & rm, Algo<int>* & the1, const bool & rob1); //error: Algo is not a template
You are passing either 2, or 4 parameters. There is no matching overload.
You need to provide at least the first 7 parameters.

Related

C++ run-time template constant

I was trying to create a user-defined container type with using and private members in the class, which are not compile-time constants. The is not valid and complained by the compiler. Those private member constants will only be initialized at runtime. Is there such a remedy for this kind of scenario?
#include <vector>
#include <array>
#include <string>
#include <iostream>
class SudokuBoard
{
private:
Board _board_data;
int _BOX_SIZE;
int _BOARD_SIZE;
const int _MAX_VALUE = _BOARD_SIZE;
const int _NUM_CONSTRAINTS = 4;
public:
template <class T, int ROW, int COL>
using Matrix = std::array<std::array<T, COL>, ROW>;
using CoverMatrix = Matrix<int, _BOARD_SIZE * _BOARD_SIZE * _MAX_VALUE, _BOARD_SIZE * _BOARD_SIZE * _NUM_CONSTRAINTS>;
using Board = Matrix<int, _BOARD_SIZE, _BOARD_SIZE>;
SudokuBoard(const std::string& filename);
void set_board_data(int row, int col, int num) { _board_data[row][col] = num; }
int get_board_data(int row, int col) const { return _board_data[row][col]; }
Board get_board_data() const { return _board_data; }
int at(int row, int col) const { return _board_data[row][col]; }
// Prints the Sudoku board
friend void print_board(const SudokuBoard& board);
friend std::ostream& operator<< (std::ostream &out, const SudokuBoard& board);
int indexInCoverMatrix(int row, int col, int num);
int createBoxConstraints(CoverMatrix coverMatrix, int header);
int createColumnConstraints(CoverMatrix coverMatrix, int header);
int createRowConstraints(CoverMatrix coverMatrix, int header);
int createCellConstraints(CoverMatrix coverMatrix, int header);
void createCoverMatrix(CoverMatrix coverMatrix);
void convertToCoverMatrix(Board board, CoverMatrix coverMatrix);
};
The first issue you get, compiling this, with a placeholder class Board{}; is something like:
Compilation failed
For more information see the output window
x86-64 clang 12.0.1 - 405ms
:23:37: error: invalid use of non-static data member '_BOARD_SIZE'
using CoverMatrix = Matrix;
What this tells you is that the compiler needs to know at compile them about the sizes and types of your templates.
A first step is to make them static const. That way, the compiler knows at compile time:
static const int _BOARD_SIZE = 9;
static const int _MAX_VALUE = _BOARD_SIZE;
(constexpr give you other, more modern, options but this is a starting point)
Then, you'll probably want to have your SudokuBoard class itself templated on some type T. But I can't get there as you didn't provide the Board class.

C++ Typedef inside classes using private members

I tried to customize some types inside a class by using private members, which are used to set the size of customized containers. But I received:
error: ‘CoverMatrix’ does not name a type
error: ‘Board’ does not name a type
How do I properly manipulate it?
#include <vector>
#include <array>
#include <string>
#include <iostream>
class SudokuBoard
{
private:
Board _board_data;
int _BOX_SIZE;
int _BOARD_SIZE;
const int _MAX_VALUE = _BOARD_SIZE;
const int _NUM_CONSTRAINTS = 4;
public:
template <class T, int ROW, int COL>
using Matrix = std::array<std::array<T, COL>, ROW>;
typedef CoverMatrix Matrix<int, _BOARD_SIZE * _BOARD_SIZE * _MAX_VALUE, _BOARD_SIZE * _BOARD_SIZE * _NUM_CONSTRAINTS>;
typedef Board Matrix<int, _BOARD_SIZE, _BOARD_SIZE>;
SudokuBoard(const std::string& filename);
void set_board_data(int row, int col, int num) { _board_data[row][col] = num; }
int get_board_data(int row, int col) const { return _board_data[row][col]; }
Board get_board_data() const { return _board_data; }
int at(int row, int col) const { return _board_data[row][col]; }
// Prints the Sudoku board
friend void print_board(const SudokuBoard& board);
friend std::ostream& operator<< (std::ostream &out, const SudokuBoard& board);
int indexInCoverMatrix(int row, int col, int num);
int createBoxConstraints(CoverMatrix coverMatrix, int header);
int createColumnConstraints(CoverMatrix coverMatrix, int header);
int createRowConstraints(CoverMatrix coverMatrix, int header);
int createCellConstraints(CoverMatrix coverMatrix, int header);
void createCoverMatrix(CoverMatrix coverMatrix);
void convertToCoverMatrix(Board board, CoverMatrix coverMatrix);
};
You already use using correctly to set up the Matrix type. You really should just keep doing that with the other types too:
using CoverMatrix = Matrix<int, _BOARD_SIZE* _BOARD_SIZE* _MAX_VALUE, _BOARD_SIZE* _BOARD_SIZE* _NUM_CONSTRAINTS>;
using Board = Matrix<int, _BOARD_SIZE, _BOARD_SIZE>;
But if you for some reason insist on mixing using and typedef, then the correct syntax is:
typedef Matrix<int, _BOARD_SIZE* _BOARD_SIZE* _MAX_VALUE, _BOARD_SIZE* _BOARD_SIZE* _NUM_CONSTRAINTS> CoverMatrix;
typedef Matrix<int, _BOARD_SIZE, _BOARD_SIZE> Board;
In other words:
using identifier = type;
typedef type identifier;
Also, do note that as #NathanOliver pointed out, your private int members are not compile time constants. So after you fix the typedef syntax the code still won't work - but you can put that in a new question if you need help with that. StackOverflow prefers only one topic per question.

Using functions within functions

Below is a piece of code I was trying to run where I have function I want to run (dN) within my main function which returns a value of type complex<double>.
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
const complex<double> Im1(0.0,1.0); //imaginary number definition
class Functions{
public:
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
complex<double> OUT = Im1*(N[k][i]+kN)/(T1);
return OUT;
};
};
int main(int argc, const char * argv[]) {
//...more code here
complex<int> **NM = new complex<int>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<int>[500];
}
complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.);
return 0;
};
Although this code does not run and returns the error: Call to non-static member function without an object argument
Based on my understanding, C++ does not allow nested functions and my code above would not work because I call a separate function in my main function. Although (based on the link) it does appear one can implement "local classes" by defining a function within a struct which would have to be inside the main function. Although when I try doing that:
int main(int argc, const char * argv[]) {
complex<int> **NM = new complex<int>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<int>[500];
}
struct Functions{
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
complex<double> OUT = Im1*(N[k][i]+kN)/(T1);
return OUT;
};
};
complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.);
return 0;
};
the error persists. Ultimately, I am simply wanting to use the function dN that returns output of type complex<double> within my main function, but am unsure of the best/operational way to implement this.
I believe you misunderstand what a nested function is.
A nested function would look like this:
int main()
{
void nested() {} // not allowed in C++
}
The solution to your problem is in the error message provided by your compiler:
Call to non-static member function without an object argument
Take a look at the following:
// Example 1
struct Functions {
void func() {}
};
int main()
{
// to call Functions::func() you would need to have an object
// of type Functions because Functions::func() is not a static function
Functions f;
f.func();
}
// Example 2
// by making func() static you can call it without an object:
struct Functions {
static void func() {}
};
int main()
{
Functions::func(); // OK
}
Ultimately, I am simply wanting to use the function dN that returns output of type complex within my main function, but am unsure of the best/operational way to implement this.
Use a free function, like main is, unless dN has a specific reason to be part of a class:
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1)
{
...
}
int main(int argc, const char * argv[]) {
...
//like this
complex<double> dN_OUT = dN(NM,1,20,0.,20.);
//not like this
//complex<double> dN_OUT = dN(**NM,1,20,0.,20.);
}
Option 1:
You can do this with class like below
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
const complex<double> Im1 (0.0, 1.0); //imaginary number definition
class Functions {
public:
complex<double> dN (complex<double> **N, int k, int i, complex<double> kN, double T1)
{
complex<double> OUT = Im1*(N[k][i] + kN) / (T1);
return OUT;
};
};
int main (int argc, const char * argv[]) {
//...more code here
complex<double> **NM = new complex<double>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<double>[500];
}
Functions fun; //create class instance
//call the function NOTE the changes here i.e not correct passing **NM
complex<double> dN_OUT = fun.dN (NM, 1, 20, 0., 20.);
return 0;
};
Option 2 (mentioned by others direct calling with changes instead of **NM you should use NM.
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
...
}
int main(int argc, const char * argv[]) {
...
complex<double> dN_OUT = dN(NM,1,20,0.,20.);
}

error: 'CvTrackers' has not been declared

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cvblob.h>
#include <iostream>
using std::string;
#include <cstdlib>
class ImageProcessing
{
// ImageProcessing* ImgPtr;
private:
IplImage *img0, *img1;
IplImage* ReducImg;
IplImage* ReducImgColor;
CvSize imgSize;
CvPoint C1,C2;
public:
friend class CarVideoHandler;
ImageProcessing(void);
~ImageProcessing(void);
void checkZone(CvTracks::const_iterator ot, double position1, double position2, int **T1,int **T2,int **T3,int **T4, int &numCars, int dire, int p, string type,string dc, string dp);
void checkZoneOneLine(CvTracks::const_iterator ot, double position1, int **T1,int **T2, int &numCars, int dire, int p, string type, int **distance,string dc, string dp);
int CarOrPerson(long area, float wh, CvTracks::const_iterator ot, int *labelTrack[]);
string idTrackCarPerson(IplImage *colourImage1, int *labelTrack[], CvBlobs::const_iterator it, CvTracks::const_iterator ot, int percent, double lin, double position1, double position2, CvFont font, char* wow, int **T1, int **T2, int **T3, int **T4, int &numCars, int &numPersons, int *Xx, int *Xy, int *Yx, int *Yy, int *dire, int n, int m, int p,string dc, string dp);
};
Compiler is throwing error 'CvTrackers' has not been declared when I compile. The same function when I declare it outside the class the compiler was not throwing any error. But when I make it class member the compiler is throwing error. Guys help me to resolve this error.
Ok so I looked through the CVBlobs.h file on Github and the iterator used in the for loops in the relevant struct is CvTracks::iterator. I would suggest trying to remove the const_ and see if that works.

Why does my derived class constructor not work?

I have been working to create an Arduino library to control a touchscreen. My library builds off of pre-existing libraries written to interface with the display & touch controllers.
Here is the constructor that I have been working on:
Display.cpp
Display::Display(int displayCSPin, int displayDCPin, int touchCSPin,
int newBacklightPin, int newRotation, int newBrightness)
: Adafruit_HX8357(displayCSPin, displayDCPin, -1),
Adafruit_STMPE610(touchCSPin)
{
//Initialize display
}
Display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include "arduino.h"
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"
#include "Adafruit_STMPE610.h"
class Display : public Adafruit_HX8357, Adafruit_STMPE610 {
public:
Display(int displayCSPin, int displayDCPin, int touchCSPin,
int newBacklightPin, int newRotation, int newBrightness);
};
Whenever I try to compile, the compiler ignores the variables in the base class constructors, and tries to call a default constructor with no variables:
error: no matching function for call to 'Adafruit_HX8357::Adafruit_HX8357()'
I have tried my best to solve this problem, but have not had any success.
Any and all help is greatly appreciated!
Here is the raw code:
Display.cpp
#include "Display.h"
Display::Display(int displayCSPin, int displayDCPin, int touchCSPin, int newBacklightPin, int newRotation, int newBrightness) : Adafruit_HX8357(displayCSPin, displayDCPin, -1), Adafruit_STMPE610(touchCSPin)
{
// tft = Adafruit_HX8357(displayCSPin, displayDCPin, -1);
//ts = Adafruit_STMPE610(touchCSPin);
tft.begin(HX8357D);
ts.begin();
tft.setRotation(newRotation);
backlightPin = newBacklightPin;
pinMode(backlightPin, OUTPUT);
rotation = newRotation;
backgroundColor = HX8357_BLACK;
brightness = newBrightness;
}
Display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include "arduino.h"
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"
#include "Adafruit_STMPE610.h"
class Display : public Adafruit_HX8357, public Adafruit_STMPE610 {
public:
Display(int displayCSPin, int displayDCPin, int touchCSPin, int newBacklightPin, int newRotation, int newBrightness);
void wake();
void sleep();
bool isAwake();
void setBackGroundColor(int newColor);
int getBackgroundColor();
void setBrightness(int newBrightness);
int getBrightness();
void setRotation(int newRotation);
int getRotation();
bool isTouched();
Adafruit_HX8357 tft;
Adafruit_STMPE610 ts;
int backgroundColor;
private:
int brightness;
int rotation;
int backlightPin;
bool awake;
bool touched;
TS_Point p;
};
Look at the actual constructors available for Adafruit_HX8357 and Adafruit_STMPE610:
Adafruit_HX8357(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK,
int8_t _RST, int8_t _MISO);
Adafruit_HX8357(int8_t _CS, int8_t _DC, int8_t _RST = -1);
Adafruit_STMPE610(uint8_t cspin, uint8_t mosipin, uint8_t misopin, uint8_t clkpin);
Adafruit_STMPE610(uint8_t cs);
Adafruit_STMPE610(void);
Your Display constructor is trying to call the second constructor of each class. However, Display is passing int values where int8_t and uint8_t values are expected. It sounds like maybe your compiler is not performing implicit conversions from int to (u)int8_t, so try using explicit conversions to force it:
Display::Display(int displayCSPin, int displayDCPin, int touchCSPin, int newBacklightPin, int newRotation, int newBrightness)
: Adafruit_HX8357((int8_t)displayCSPin, (int8_t)displayDCPin, -1),
Adafruit_STMPE610((uint8_t)touchCSPin)
{
//Initialize display
}
Otherwise, change you Display constructor parameters to use int8_t and uint8_t instead of int:
Display::Display(int8_t displayCSPin, int8_t displayDCPin, uint8_t touchCSPin, int newBacklightPin, int newRotation, int newBrightness)
: Adafruit_HX8357(displayCSPin, displayDCPin, -1),
Adafruit_STMPE610(touchCSPin)
{
//Initialize display
}