error C2062 : type int unexpected - c++

I am new to c++ and SWIG
I am creating a python module using SWIG in windows environment.
After creating wrapper class (example_wrap.cxx). Started building using (python setup.py build_ext --inplace) for creating python module.
But I am getting *example_wrap.cxx(3090) : error C2062: type 'int' unexpected*
GradedComplex.h:
class GradedComplex
{
public:
typedef std::complex<double> dcomplex;
typedef Item<dcomplex> item_type;
typedef ItemComparator<dcomplex> comparator;
typedef std::set<item_type, comparator> grade_type;
private:
int n_;
std::vector<grade_type *> grade_;
std::vector<double> thre_;
public:
GradedComplex(int n, double *thre);
~GradedComplex();
void push(item_type item);
void avg(double *buf);
};
#endif
GradedComplex.cc
GradedComplex::GradedComplex(int n, double *thre)
{
n_ = n;
for (int i = 0; i < n_; ++i)
{
thre_.push_back(thre[i]);
grade_.push_back(new grade_type());
}
}
Then I build it for generating python module using SWIG.
Swig interface file (example.i)
GradedComplex(int n, double *thre);
I am not much expert in SWIG interface file
The wrapper class generated has large volume of code so i am pasting few.
code : example_wrap.cxx
3083: #define SWIG_FILE_WITH_INIT
3084: #include "Item.h"
3085: #include "GradedComplex.h"
3086: typedef std::complex<double> dcomplex;
3087: typedef Item<dcomplex> item_type;
3088: typedef ItemComparator<dcomplex> comparator;
3089: typedef std::set<item_type, comparator> grade_type;
3090: GradedComplex(int n, double *thre);
3091: void push(item_type item);
3092: void avg(double *buf);
3093: #include <string>
3094: #include <complex>
3095: #include <iostream>
3096: #if PY_VERSION_HEX >= 0x03020000
3097: # define SWIGPY_SLICE_ARG(obj) ((PyObject*) (obj))
3098: #else
3099: # define SWIGPY_SLICE_ARG(obj) ((PySliceObject*) (obj))
3100: #endif
The GradedComplex constructor:
GradedComplex::GradedComplex(int n, double *thre)
{
n_ = n;
for (int i = 0; i < n_; ++i)
{
thre_.push_back(thre[i]);
grade_.push_back(new grade_type());
}
}
Please suggest a to rectify this error

You apparently declared class GradedComplex somewhere in some header file (GradedComplex.h?)
Later you attempted to use this name in this line
GradedComplex(int n, double *thre);
To a human reader this line would probably look like an attempt to declare an independent function GradedComplex. Technically, it is legal to have a function with the same name as an existing class. However, since you specified no return type for this function, the compiler does not see this as a function declaration. The compiler thinks you are trying to declare an object of type GradedComplex with redundant parentheses around the declarator, as in
GradedComplex (a);
For this reason, the appearance of that int confuses it and leads to an error report about an unexpected int in line 3090.
What were you trying to do? If you were trying to define a constructor for GradedComplex, then you already know how to do it (you posted a correct definition yourself). What is the purpose of line 3090? Why did you write that line?

You can not have a function with no return type in c++. You should set a return type for the function GradedComplex. Constructors can not be declared like that.

Related

Lambda in header file error

In one of my classes, I am trying to use std::priority queue with a specified lambda for comparison:
#pragma once
#include <queue>
#include <vector>
auto compare = [] (const int &a, const int &b) { return a > b; };
class foo
{
public:
foo() { };
~foo() { };
int bar();
private:
std::priority_queue< int, std::vector<int>, decltype(compare)> pq;
};
My program compiles perfectly until I add a .cpp file to accompany the header:
#include "foo.h"
int foo::bar()
{
return 0;
}
This time, my compiler generates an error:
>main.obj : error LNK2005: "class <lambda> compare" (?compare##3V<lambda>##A) already defined in foo.obj
Why can't I create a accompanying .cpp file if my header file contains a lambda?
Compiler: Visual Studio 2012
My main.cpp:
#include "foo.h"
int main(){
return 0;
}
As #Rapptz suggested,
const auto compare = [] (const int &a, const int &b) { return a > b; };
Solved the problem. Why?
Internal vs External linkage. By default, auto, like int has external linkage. So just how:
int j = 5;
In foo.h that would later be included by foo.cpp throws a
Error 2 error LNK2005: "int j" (?j##3HA) already defined in Header.obj
(VS 2013)
However, const makes the linkage internal by default, which means it is only accessible in one translation unit, thereby avoiding the problem.
I'm unable to replicate this problem for some reason. I'm trying this on VS2010 though - not sure if that made a difference. In fact, I tried including your header in two source files and it compiles, links and runs fine.
That said, do you want to consider using std::function. That way you can define the lambda in the cpp code and it won't get defined multiple times for whatever reason. (BTW, where's foo.obj coming from? Do you have another source file that is including this header ?).
foo.h:
#pragma once
#include <queue>
#include <vector>
#include <functional>
typedef std::function<bool (int, int) > comptype;
//auto compare = [] (const int &a, const int &b) { return a > b; };
class foo
{
public:
foo() { };
~foo() { };
int bar();
private:
std::priority_queue< int, std::vector<int>, comptype> pq;
};
Then later in the cpp include and define the lambda and when you create the pq pass it to the constructor.
foo.cpp:
auto compare = [] (const int &a, const int &b) { return a > b; };
foo::foo():pq(compare){}
This way you're deftly not defining the function multiple times.

error: expected initializer before '<' token

I'm trying to have MAGMA as backend for Eigen in the same way it does already support MKL. While doing so I bump into the error above. The relevant snippets below:
template <>
/*ERROR IN THIS LINE >>>>>*/ inline void assign_scalar_eig2magma<magmaDoubleComplex,dcomplex>(magmaDoubleComplex& magmaScalar, const dcomplex& eigenScalar) {
magmaScalar.x=eigenScalar.real();
magmaScalar.y=eigenScalar.imag();
}
and the magmaDoubleComplex is defined in magma_types.h:
// ========================================
// define types specific to implementation (CUDA, OpenCL, MIC)
// define macros to deal with complex numbers
#if HAVE_CUBLAS
#include <cublas.h>
typedef cudaStream_t magma_queue_t;
typedef cudaEvent_t magma_event_t;
typedef int magma_device_t;
typedef cuDoubleComplex magmaDoubleComplex;
typedef cuFloatComplex magmaFloatComplex;
As far as I can see magmaDoubleComplex is declared but this doesn't seem to be the issue here ...
UPDATE: Indeed my mistake, here the template definition is "mkl" and it should be "magma".
template<typename MAGMAType, typename EigenType>
static inline void assign_scalar_eig2mkl(MAGMAType& magmaScalar, const EigenType& eigenScalar) {
magmaScalar=eigenScalar;
}

Invalid template arguments on map std::map< std::string, Stock*> &stocks

I have the declaration (or similar)
std::map< std::string, Stock*> &stocks;
throughout my code. Eclipse does not like this and produces a "Invalid template arguments" error.
Stock is declared as:
class Stock {
public:
Stock(std::string, qbbo::Financial_status_indicator, qbbo::Security_class,
qbbo::Current_trading_state,
qbbo::Market_category, qbbo::Reg_sho_action);
~Stock();
void setFinancialStatusIndicator(qbbo::Financial_status_indicator financialStatusIndicator);
void setSecurityClass(qbbo::Security_class securityClass);
void setCurrentTradingState(qbbo::Current_trading_state tradingState);
void setMarketCategory(qbbo::Market_category marketCategory);
void setREGShoAction(qbbo::Reg_sho_action regSHOAction);
bool isStockTrading();
private:
enum StockState {
STOCK_STATE_OK, STOCK_STATE_UNKNOWN, STOCK_STATE_UNEXPECTED_CHARACTERISTIC
};
std::string name;
int inventory;
StockState currentState;
// Expected values initialised in constructor
qbbo::Financial_status_indicator expectedFinancialStatusIndicator;
qbbo::Security_class expectedSecurityClass;
qbbo::Current_trading_state expectedCurrentTradingState;
qbbo::Market_category expectedMarketCategory;
qbbo::Reg_sho_action expectedRegSHOAction;
// Actual values as set by messages
qbbo::Financial_status_indicator financialStatusIndicator;
qbbo::Security_class securityClass;
qbbo::Current_trading_state currentTradingState;
qbbo::Market_category marketCategory;
qbbo::Reg_sho_action regSHOAction;
void nextState();
};
I cannot see whats invalid about this declaration and it compiles fine. Is there something I'm missing and Eclipse is catching?
Short Self Contained Correct Example
#include <string>
#include <map>
#include "stock.h"
int main() {
std::map<std::string, Stock*> stocks;
}
Turned out to be an eclipse error. Creating a new project and re-following the steps Eclipse CDT C++11/C++0x support sorted it.

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)
{
....
}

'Scanner' does not name a type error in g++

I'm trying to compile code in g++ and I get the following errors:
In file included from scanner.hpp:8,
from scanner.cpp:5:
parser.hpp:14: error: ‘Scanner’ does not name a type
parser.hpp:15: error: ‘Token’ does not name a type
Here's my g++ command:
g++ parser.cpp scanner.cpp -Wall
Here's parser.hpp:
#ifndef PARSER_HPP
#define PARSER_HPP
#include <string>
#include <map>
#include "scanner.hpp"
using std::string;
class Parser
{
// Member Variables
private:
Scanner lex; // Lexical analyzer
Token look; // tracks the current lookahead token
// Member Functions
<some function declarations>
};
#endif
and here's scanner.hpp:
#ifndef SCANNER_HPP
#define SCANNER_HPP
#include <iostream>
#include <cctype>
#include <string>
#include <map>
#include "parser.hpp"
using std::string;
using std::map;
enum
{
// reserved words
BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID,
// punctuation and operators
LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES,
DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE,
// symbolic constants
NUM, ID, ENDFILE, ERROR
};
class Token
{
public:
int tag;
int value;
string lexeme;
Token() {tag = 0;}
Token(int t) {tag = t;}
};
class Num : public Token
{
public:
Num(int v) {tag = NUM; value = v;}
};
class Word : public Token
{
public:
Word() {tag = 0; lexeme = "default";}
Word(int t, string l) {tag = t; lexeme = l;}
};
class Scanner
{
private:
int line; // which line the compiler is currently on
int depth; // how deep in the parse tree the compiler is
map<string,Word> words; // list of reserved words and used identifiers
// Member Functions
public:
Scanner();
Token scan();
string printTag(int);
friend class Parser;
};
#endif
anyone see the problem? I feel like I'm missing something incredibly obvious.
parser.hpp incluser scanner.hpp and vice versa.
So one file evalated before the other.
You can use a forward declaration like
class Scanner;
or reorginaze your headers
You are including Scanner.hpp in Parser.hpp and you are also including Parser.hpp in Scanner.hpp.
If you include Scanner.hpp in your source file then the definition of the Parser class will appear before the definition of the Scanner class and you will get the error you are seeing.
Resolve the circular dependency and your problem will go away (headers should never circularly depend on each other for types).
You have circular #include reference: one header file includes another and vice versa. You need to break this loop somehow.