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.
Related
Before the assignment operation, the member variable u works fine, see figure 1
After the assignment operation, the data of the member variable u is tampered, see figure 2
Continuing resize operation will trigger an error: read access violation, see figure 3
The development environment is win10 Visual Studio Community 2022 17.0.4 x64 in Debug mode
Compiling by mingw does not show this problem and the code runs smoothly. I suspect it's a bug for msvc. In addition, this error is very strange, if merged the 4 code files, this error may or may not disappear, which is related to the code order.
Below is the complete code
test.h
#pragma once
class ETEST {
public:
ETEST() {};
void add() {};
};
order.h
#pragma once
#include <vector>
class ETEST;
class First {
public:
First(const char*);
void (ETEST::* add)();
std::vector<double> u, v;
};
class Second : public First {
public:
Second(const char*);
};
first.cpp
#include "test.h"
#include "order.h"
First::First(const char*) :
add(NULL), u{}, v{} {
};
int main(int argc, char* argv[]) {
Second chk("");
return 0;
}
second.cpp
#include "order.h"
#include "test.h"
#include <cstdio>
Second::Second(const char*) :
First(NULL) {
add = &ETEST::add;
u.resize(10, 0.0);
printf("u resize sucess!\n");
};
Below are my actions:
1.create new solution
2.add code files
3,paste the code
4.Debugging, it throws an exception
(I did not modify any solution properties, all use the default settings)
There's the main.cpp that has lots of Log prinkled wherever:
#include "utils.hpp"
...//some code
int main(){
int a = 0;
int b = 0;
util::LogClass::Log("Initial","something);
//some more code
util::LogClass::Log("Mid","something");
//some more code
util::LogClass::Log("Middle","something");
}
And the LogClass is defined like this in utils.hpp:
namespace util{
class LogClass{
public:static bool LOG_ENABLED;
public: static void Log(std::string tag, std::string message){
if(LOG_ENABLED){
std::cerr << tag+": "+message <<std::endl;}
}
}
bool LogClass::LOG_ENABLED=true;
}
I was thinking I would be able to do this in main.cpp:
#include "utils.cpp"
util::LogClass::LOG_ENABLE=false;
int main(){ //the previous main function}
*the above code actuallly gives an error saying: redefinition of ‘bool util::LogClass::LOG_ENABLED’
bool util::LogClass::LOG_ENABLE=false *
but, if I move it inside the main:
#include "utils.cpp"
int main(){ util::LogClass::LOG_ENABLED=false; //the previous main function}
then the code compiles fine. So my question is why can't I enable it outside the main() function even if it is a static member, and why does the (g++) compiler takes it as a redefinition?
You can only statically initialize a variable at the point where it is getting defined. The initialization inside the main function is dynamic, so that's fine.
I agree that the compiler error is weird though - the compiler might be trying to auto-deduct the "missing" type that should be there for a redefinition.
I am having trouble in my C++ code where I have to make a binary heap. It works fine as long as I had the "main" function inside of my "MyHeap.h" file but my professor wants it to run in a separate test file. For some reason the code doesn't want to run whenever I try to put the main function outside of the "MyHeap.h" file. When it runs I get the following error:
error C2143: syntax error: missing';' before '<'
I looked at my code and this is where it says there is an error but I can't see anything.
// MyHeap.h
#ifndef _MYHEAP_H
#define _MYHEAP_H
#include <vector>
#include <iterator>
#include <iostream>
class Heap {
public:
Heap();
~Heap();
void insert(int element);
int deletemax();
void print();
int size() { return heap.size(); }
private:
int left(int parent);
int right(int parent);
int parent(int child);
void heapifyup(int index);
void heapifydown(int index);
private:
vector<int> heap;
};
#endif // _MYHEAP_H
So like I said whenever I have the the int main function right after the private class, it will work just fine. Now when I implement it into my test file which is this:
#include "MyHeap.h"
#include <vector>
#include <iostream>
int main()
{
// Create the heap
Heap* myheap = new Heap();
myheap->insert(25);
myheap->print();
myheap->insert(75);
myheap->print();
myheap->insert(100);
myheap->print();
myheap->deletemax();
myheap->print();
myheap->insert(500);
myheap->print();
return 0;
}
It keeps popping up the errors, any ideas on I could go about fixing this problem so that my code can run from a test file?
Use std::vector instead of vector.
The compiler is complaining it doesn't know about vector.
Since it lives in std namespace, the safest solution is to prefix with std.
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.
I am trying to implement the a map from the C++ STL as follows:
#include <string>
#include <iostream>
#include <map>
using namespace std;
#include "assembler.h"
// This Class makes use of the Map Template from the Standart Template Library
// All addresses are stored as numerical (Dec) integers
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
int address = 0;
}
void SymbolTable::addEntry(string symbol, int address) {
symbolTable[symbol] = address;
address++;
}
// Returns true if symbolTable already contains symbol
bool SymbolTable::contains(string symbol) {
if (symbolTable.find(symbol) == symbolTable.end()) { return true; }
else { return false; }
}
int SymbolTable::getAddress(string symbol) {
return symbolTable[symbol];
}
I try to compile this with
c++ *.cpp -0 assembler.out
and I get the following error message:
symboltable.cpp:57:9: error: no viable conversion from 'mapped_type' (aka 'std::basic_string<char>') to 'int'
return symbolTable[symbol];
^~~~~~~~~~~~~~~~~~~
1 error generated.
I have searched for this error online and all I get is bug reports relating to the STL and I cannot figure out if those reports are the same problem I am having and if so how to get around it. Am I doing something wrong?
I have tried (probably stupidly) to typecast the offending line as
return (int) symbolTable[symbol];
Thank you for any help.
My header file declares the class as:
class SymbolTable {
public:
SymbolTable();
void addEntry(string, int);
bool contains(string);
int getAddress(string);
private:
map <string, string> symbolTable;
int address;
};
This:
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
^
^
is a function-local variable, not a member variable. It is not the same as the symbolTable that you're accessing in e.g. getAddress, which presumably is a member variable. You haven't shown the class body, but my guess is that it's defined differently.