error: expected primary-expression before '<' token - c++

I compiled the below code with gcc4.4.1 on linux:-
#include "glob.h"
#include "netlist.h"
#include "netlist_params.h"
#include "netlist_abbrev.h"
#include "lvs_util.h"
#include "lvs_report.h"
#include "lvs_data.h"
#include "compare_opts.h"
#include "flatten.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include "hash.h"
static THREAD_PRIVATE NlStringVec* ext_str_tab_v;
static THREAD_PRIVATE const char* ext_str_tab_s1;
static THREAD_PRIVATE const char* ext_str_tab_s2;
int Netlist::ExtStrHash::operator () (NlString i) const {
if (i.is_valid())
ext_str_tab_s1 = (*ext_str_tab_v)[i];
return Hash<const char*>::operator()(ext_str_tab_s1);
}
Error:-
netlist_back_1.C: In member function 'int Netlist::ExtStrHash::operator()(NlString) const':
netlist_back_1.C:24: error: expected primary-expression before '<' token
netlist_back_1.C:24: error: expected primary-expression before 'const'
netlist_back_1.C:24: error: expected ';' before 'const'
netlist_back_1.C:24: error: expected unqualified-id before '>' token
netlist_back_1.C:24: error: expected initializer before '>' token
Definition of hash in file hash.h:-
namespace Hash {
// template <class Key> struct Hash { };
#define DECL_SIMPLE_HASH(type) \
template <> \
struct Hash<type> { \
unsigned int operator() (type x) const { return x; } \
}
DECL_SIMPLE_HASH(signed char);
DECL_SIMPLE_HASH(unsigned char);
DECL_SIMPLE_HASH(signed short);
DECL_SIMPLE_HASH(unsigned short);
DECL_SIMPLE_HASH(signed int);
DECL_SIMPLE_HASH(unsigned int);
DECL_SIMPLE_HASH(signed long);
DECL_SIMPLE_HASH(unsigned long);
DECL_SIMPLE_HASH(signed long long);
DECL_SIMPLE_HASH(unsigned long long);
#undef DECL_SIMPLE_HASH
template <>
class Hash<const char*> {
static const int M = 61; // 5;
public:
unsigned int operator() (const char* s) const {
// case insensitive, so sensitivity can be turned on/off without
// effecting hash #.
unsigned h = 0;
char c;
while (c = *s++) {
if (c >= 'A' && c <= 'Z')
c = c - 'A' + 'a';
h = M*h + c;
}
return h;
}
};
template <> struct Hash<char*> : public Hash<const char*> {};
}
EDIT:: I needed to include the namespace. Working on other's code lead to foolish mistakes.
Thanks for vote down and help. :)
Any help is appreciated. Thanks.

As far as I can see:
You're not including "hash.h"
You've commented out the declaration of the Hash template, leaving only the explicit specialisations. You still need to declare the template, even if you don't define the generic version.
The Hash template is inside a namespace Hash, but you neither use that namespace nor fully qualify the template name. (By the way, it's not a very good idea to use the same name for a namespace and a class; it can lead to ambiguities).
You're trying to call a non-static operator() as if it's static; you want something more like return Hash<const char*>()(ext_str_tab_s1);.
So it's no surprise that Hash is not recognised as a template name (although it has to be said that the error message is not very helpful).

There are few possible options :
you didn't include the header where the Hash template is defined
the template Hash is in different namespace

Hash is a namespace. Namespaces can't have template parameters.
Don't give types and namespaces the same name.

Related

skipping adding constructors when inheriting from std::string class

Tried to argument the std::string so that it supports method "bool operator==(int)". I got errors:
$ g++ -std=c++11 te2.cc
te2.cc: In function ‘int main(int, char**)’:
te2.cc:20:20: error: no matching function for call to ‘mstring::mstring(const char [4])’
te2.cc:20:20: note: candidates are:
te2.cc:10:7: note: mstring::mstring()
te2.cc:10:7: note: candidate expects 0 arguments, 1 provided
te2.cc:10:7: note: mstring::mstring(const mstring&)
te2.cc:10:7: note: no known conversion for argument 1 from ‘const char [4]’ to ‘const mstring&’
te2.cc:10:7: note: mstring::mstring(mstring&&)
te2.cc:10:7: note: no known conversion for argument 1 from ‘const char [4]’ to ‘mstring&&’
Here is the simple source:
#include <unordered_map>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
class mstring : public string {
public:
//mstring (char* p) : std::string(p) {};
bool operator == (int x) {
int n = atoi(this->c_str());
return (n == x);
}
};
int main(int argc, char *argv[])
{
mstring t("123");
if (t == atoi(argv[1])) {
printf("yes\n");
} else {
printf("no\n");
}
}
If I uncomment the constructor /mstring (char* p) : std::string(p) {};, then it compiles and runs fine.
The question is, if it possible to make it work without defining the constructors for mstring, just use the whatever the constructors of the base class (there is no new data member anyway)? Thanks.
What about providing a free standing operator function instead of inheriting from std::string (which makes that code more usable overall):
bool operator==(const std::string& s, int i) {
int n = atoi(s.c_str());
return (n == i);
}
bool operator==(int i, const std::string& s) {
return s == i;
}
Or even more generic:
template<typename T>
bool operator==(const std::string& s, T t) {
std::istringstream iss;
iss << t;
return (s == iss.str());
}
Classes from the std namespace aren't intended to be inherited, but just used in interfaces and function parameters. Inheriting from those classes makes your code less usable, since clients need to use your implementation instead of just using the std type.
Also note: For your particular use case it's not necessary to convert anything at all, unless you want to assert that argv[1] contains a number (where atoi() certainly isn't the best method to do so, look up stoi() instead). You can just compare the strings:
if (std::string("123") == argv[1]) {
printf("yes\n");
} else {
printf("no\n");
}
you can explicitly inherit the constructors by adding
using string::string;
in your class

static members get 'is not a static member of class'

Problem
I want to assign values in a class definition which is in a separate header file from the class declaration cpp.
On compilation I recieve theses error messages:
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mRegexes’ is not a static member of ‘class bob’const std::map<uint,std::string> bob::mRegexes = {
^
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mResponses’ is not a static member of ‘class bob’ const std::map<uint,std::string> bob::mResponses = {
both of which have been absolutely infurriating because I do not understand why the compiler is ignoring thetypedef for std::string I feel like I'm missing something here but I'm not sure why the bob.h file is seeing the parameters differently than the bob.cpp.
bob.h
#ifndef BOB_H
#define BOB_H
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <map>
typedef unsigned int uint;
// This was first to go when I started having problems.
/*using std::string;*/
using std::map;
// boost::regex > c++11::regex (gcc doesn't follow standards).
using boost::regex;
class bob
{
enum respond_to{
QUESTION,
YELL,
NAME,
DEFAULT,
LENGTH
};
public:
static const respond_to mResponseTypes;
static const map<uint,std::string> mRegexes;
static const map<uint,std::string> mResponses;
static std::string hey(std::string sentence);
static const std::string* evaluate (const std::string& sentence);
static const std::string* getResponse(const std::string& sentence, const respond_to& type) noexcept(true);
};
#endif
bob.cpp
#include "bob.h"
const std::map<uint,std::string> bob::mRegexes = {
{QUESTION, "[a-z]+\\?"},
{YELL,"[A-Z]+"}
};
const std::map<uint,std::string> bob::mResponses = {
{QUESTION,"Sure"},
{YELL,"Whoah, chill out!"},
{DEFAULT,"Whatever."}
};
// ...

C++: strange missing ';' before error, due to a typedef'ed return type in templated class

I am getting a very confusing compiler error when building the following test code.
f:\data\sdks\smctc-1.00\examples\ik_pf\msvc\MarkerSampler.inl(16): error C2143: syntax error : missing ';' before 'MarkerSampler<MarkerT>::sample2'
f:\data\sdks\smctc-1.00\examples\ik_pf\msvc\MarkerSampler.inl(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Note sample builds without error, but sample2 is causing the problem. The only difference being one explicitly details the return type without the use of a typedef and the other uses the typdef'ed version.
.h file
#pragma once
#ifndef __MARKERSAMPLER_H_INCLUDED__
#define __MARKERSAMPLER_H_INCLUDED__
#include <vector>
#include <string>
#include <qthread.h>
#include <Wm5Vector3.h>
#include "UtilityFunctions.h"
#include "Marker.h"
#include "MarkerSet.h"
template <class MarkerT>
class MarkerSampler : public QThread
{
typedef std::vector<MarkerT> MarkerVector ;
typedef MarkerSet<MarkerT> MarkerSetT ;
typedef std::vector<MarkerSetT> MarkerSetVector ;
public :
MarkerSampler(const std::string inputDataDirectory, const unsigned int nSamples, const unsigned int startFrame, const unsigned int nFramesToUse) :
inputDataDirectory_(inputDataDirectory), nSamples_(nSamples), startFrame_(startFrame), nFramesToUse_(nFramesToUse) {seed = -time(NULL) ;}
~MarkerSampler() {}
std::vector<MarkerSet<MarkerT> > sample(const double scaleFactor = 1.0, const double noiseSD = 0.0) ;
MarkerSetVector sample2(const double scaleFactor = 1.0, const double noiseSD = 0.0) ;
protected:
void run();
private:
int seed ;
const std::string inputDataDirectory_ ;
const unsigned int nSamples_ ;
const unsigned int startFrame_ ;
const unsigned int nFramesToUse_ ;
} ;
#include "MarkerSampler.inl"
#endif
.inl file
template <class MarkerT>
std::vector<MarkerSet<MarkerT> > MarkerSampler<MarkerT>::sample(const double scaleFactor, const double noiseSD)
{
....
}
template <class MarkerT>
MarkerSetVector MarkerSampler<MarkerT>::sample2(const double scaleFactor, const double noiseSD)
{
....
}
The missing ; part of that error is slightly misleading, the compiler actually just doesn't recognize the MarkerSetVector type. I think this is because it's typedef'd inside the MarkerSampler class.
If you explicitly state the scope of the typedef it should fix the error. Though (depending on your compiler at least) you may also need to add in the typename keyword, since MarkerSetVector is a dependant type. Here's a fixed example:
template <class MarkerT>
typename MarkerSampler< MarkerT >::MarkerSetVector MarkerSampler<MarkerT>::sample2(const double scaleFactor, const double noiseSD)
{
....
}

What is the meaning of this 'missing template arguments' C++ error

Ahh, C++ templates...
The code I see,
makes sense to me,
but GCC...
it disagrees.
The following code compiles and runs as expected, but if you uncomment that #define, you get the error, which I don't understand. The symbol iterator still has only one thing it can refer to: the typedef in the superclass. So I guess I have two questions: 1. What do the errors mean? 2. What is the best way to fix them.
#include <map>
#include <string>
#include <cstdio>
using namespace std;
// #define WITH_TEMPLATE 1
#ifdef WITH_TEMPLATE
template <class C>
struct MyClass : public map<string, C>
#else
struct MyClass : public map<string, int>
#endif
{
bool haskey(const string &s)
{
iterator it = find(s);
return (it != end());
}
};
int main()
{
#ifdef WITH_TEMPLATE
MyClass<int> m;
#else
MyClass m;
#endif
m["test"] = 10;
printf("%d %d\n", m.haskey("test"), m.haskey("no"));
}
Errors from GCC:
temp.cc: In member function ‘bool MyClass::haskey(const std::string&)’:
temp.cc:18: error: missing template arguments before ‘it’
temp.cc:18: error: expected `;' before ‘it’
temp.cc:19: error: ‘it’ was not declared in this scope
temp.cc:19: error: there are no arguments to ‘end’ that depend on a template parameter, so a declaration of ‘end’ must be available
temp.cc:19: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
You need to change your MyClass::haskey method too.
bool haskey(const string &s)
{
typename MyClass<C>::iterator it = this->find(s);
return (it != this->end());
}
Explanation of such a behavior is in section "Name lookup, templates, and accessing members of base classes" on http://physics.ucsd.edu/students/courses/winter2008/physics141/manuals/rhel-gcc-en-4/c---misunderstandings.html (link from another answer's comment, just in case).
Whole fixed example code: http://ideone.com/G7Rty
iterator it = find(s);
return (it != end());
This line should be as,
#ifdef WITH_TEMPLATE
typename map<string, C>::iterator it = this->find(s);
return (it != this->end());
#else
map<string, int>::iterator it = find(s);
return (it != end());
#endif

Help with error: ISO C++ forbids declaration of 'vector' with no type

As the title states, I'm not sure why I'm getting this error. I've put together a test.cpp that's similar to this structure, and it works fine. Also, other than the vector problem, there's the other problem about 'protected', which isn't even in the code. I think 'protected' is a macro, so no telling what's there. I'm new to QT, so I'm likely "doing it wrong." That's certainly what the compiler's suggesting.
In file included from DrvCrystalfontz.cpp:8:
LCDText.h:28: error: ISO C++ forbids declaration of 'vector' with no type
LCDText.h:28: error: expected ';' before '<' token
LCDText.h:30: error: ISO C++ forbids declaration of 'vector' with no type
LCDText.h:30: error: expected ',' or '...' before '<' token
LCDText.h:46: error: expected ':' before 'protected'
LCDText.h: In constructor 'LCDText::LCDText(int, int, int, int, int, int, int, QObject*)':
LCDText.h:33: error: expected '{' at end of input
scons: *** [DrvCrystalfontz.o] Error 1
scons: building terminated because of errors.
Here's the code. I've numbered the lines noted in the error.
#ifndef __LCD_TEXT__
#define __LCD_TEXT__
#include <vector>
#include <QObject>
#include "LCDBase.h"
#include "WidgetText.h"
#include "WidgetBar.h"
#include "WidgetHistogram.h"
#include "WidgetIcon.h"
#include "WidgetBignums.h"
#include "WidgetGif.h"
class LCDText: public LCDBase, public virtual QObject {
Q_OBJECT
protected:
char *LayoutFB;
char *DisplayFB;
int GOTO_COST;
int CHARS;
int CHAR0;
int LROWS;
int LCOLS;
int DROWS;
int DCOLS;
vector<vector<char *> > chars; // Line 28
void (*TextRealWrite) (const int row, const int col, const char *data, const int len);
void (*TextRealDefchar) (const int ascii, const vector<char *> matrix); // Line 30
public:
LCDText(int rows, int cols, int xres, int yres, int _goto, int chars,
int char0, QObject *parent) : LCDBase(xres, yres), QObject(parent); // Line 33
~LCDText();
void TextInit(int rows, int cols);
void TextBlit(int row, int col, int height, int width);
void TextClear();
void TextClearChars();
void TextGreet();
void TextDraw(WidgetText widget);
void TextBarDraw(WidgetBar widget);
void TextHistogramDraw(WidgetHistogram widget);
void TextIconDraw(WidgetIcon widget);
void TextBignumsDraw(WidgetBignums widget);
void TextGifDraw(WidgetGif widget);
public signals: // Line 46
void SpecialCharChanged(int ch);
public slots:
void TextSpecialCharChanged(int ch);
};
#endif
Vector resides in the std namespace. You have to do one of the following:
Prepend the type with the namespace:
std::vector<std::vector<char *> > chars;
Tell the compiler you are using vector from the std namespace
using std::vector;
vector<vector<char *> > chars;
Or, tell the compiler you are using the std namespace, which will bring in everything (not recommended, see comments)
using namespace std;
every symbol declared in C++ standard library is part of the std namespace. In order to use these declarations you have to refer it by its full name. namely std::.
As MichaelM answered you should use std::vector instead of vector.
You can, however, use the following "using declarations":
1. using std::vector;
2. using namespace std; // using namespace ...; is mostly discouraged as it causes a mass-import of symbols into the global namespace
On any case, most of the time you should avoid using declaration in header files as it pollutes the global namespace for every user of your header.
good luck