here I have an error but I don't know why it shows. This is the error:
In file included from Exploit.cc:2:0: Command.hh:35:17: error: field
‘_value’ has incomplete type Command.hh: In constructor
‘Command::Command(const char*)’: Command.hh:27:3: error: ‘_value’ was
not declared in this scope make: *** [Exploit.o] Error 1
And this is Command.hh
class Command {
public:
Command(const char* exp){
_value=exp;
_value.append("\n");
}
~Command();
void request(int fd);
void response(std::string res);
const char* getCommand();
private:
std::string _value;
};
Exploit.cc
typedef std::shared_ptr<Command> CommandPtr;
typedef std::list<CommandPtr> CommandQueue;
typedef std::shared_ptr<CommandQueue> CommandQueuePtr;
Exploit::Exploit(const char* exp, int fd2, int type2): fd(fd2), type(type2){
commands_to_execute = make_shared<CommandQueue>();
commands_executed = make_shared<CommandQueue>();
CommandPtr pr=std::make_shared<Command>( exp);
commands_to_execute->push_back(pr);
}
I hope someone could help me, because It's very weird for me.
Thank you!!
Your forgot to include the string header:
#include <string>
in Command.hh.
On a related note, maybe it's a good idea to make the constructor accept an std::string:
Command(const std::string& exp) {
instead of a const char*.
Related
I am getting this error and I can not figure it out can someone please help explain what I am doing wrong? I have tried removing some of the include statements as well as creating a makefile for my project but I keep getting the same errors
This is my Token.h
#pragma once
#include <string>
#include <iostream>
enum TokenType {
// Reserved Words:
VOID_TOKEN, MAIN_TOKEN, INT_TOKEN, COUT_TOKEN
};
// IMPORTANT: The list above and the list below MUST be kept in sync.
const std::string gTokenTypeNames[] = {
"VOID", "MAIN", "INT", "COUT"
};
class TokenClass {
private:
TokenType mType;
std::string mLexeme;
public:
TokenClass();
TokenClass(TokenType type, const std::string &lexeme);
TokenType GetTokenType() const {
return mType;
}
};
This is my Token.cpp file
#include "Token.h"
TokenClass::TokenClass() {
}
TokenClass::TokenClass(TokenType type, const std::string &lexeme) {
mType = type;
mLexeme = lexeme;
}
TokenType TokenClass::GetTokenType() const {
return mType;
}
and here is my Main.cpp file
#include "Token.h"
int main() {
TokenType tt = VOID_TOKEN;
std::string lexeme = "void";
TokenClass tok1(tt, lexeme);
// std::cout << tok1 << std::endl;
}
and here are the errors I am getting
g++ -std=c++11 -g main.cpp Token.cpp -o Token
Token.cpp:11:11: error: redefinition of ‘TokenType TokenClass::GetTokenType() const’
TokenType TokenClass::GetTokenType() const {
^~~~~~~~~~
In file included from Token.cpp:1:
Token.h:40:12: note: ‘TokenType TokenClass::GetTokenType() const’ previously defined here
TokenType GetTokenType() const {
^~~~~~~~~~~~
make: *** [Makefile:8: main] Error 1
Your error message tells you file named and lines in each file. Line 11 of the CPP and line 40 of the header file.
Delete the body (the stuff in {}) part of thr=e function in the header file )reppacing it with a ;), or delete all mention of the function in the cpp file. Both work.
How to pass a string to a method in a class?
code
class Txtbin{
private:
std::string input;
std::string output = "output.png";
void error();
public:
Txtbin();
void run();
};
Txtbin::Txtbin(){
}
void Txtbin::error(const char* str){
throw std::runtime_error(str);
}
void Txtbin::run(){
if(input == ""){
error("Input file not defined");
}
}
error
# g++ -std=c++11 txtbin.cpp -o txtbin `pkg-config opencv --cflags --libs`
txtbin.cpp:30:6: error: prototype for ‘void Txtbin::error(const char*)’ does not match any in class ‘Txtbin’
void Txtbin::error(const char* str){
^
txtbin.cpp:14:8: error: candidate is: void Txtbin::error()
void error();
^
As others mentioned, you are declaring void error(); but defining void error(const char* str);. Put const char* str parameter in the declaration too, inside the class.
prototype for ‘void Txtbin::error(const char*)’
does not match any in class ‘Txtbin’
You're trying to define Txtbin's void error(const char*) function, but it does not have one.
candidate is: void Txtbin::error()
It does, however, declare a void error() function, without the parameter. Since you actually use that parameter in the implementation, you probably want to add it to its declaration.
Like others have said, void error() requires no parameter. However later you create void error(const char* str) which has a parameter.
class Txtbin{
private:
string input;
string output = "output.png";
public:
Txtbin();
void error(const char*); //This is what you need.
/* void error(); THIS IS WHAT YOU HAD */
void run();
};
void Txtbin::error(const char* str)
{
//Whatever
}
I know there are a couple other questions on this specific question, but nothing that I can find on it seems to work, so I'm posting my specific code.
Here is the code:
#ifndef __MEMORY_TRACKER_H__
#define __MEMORY_TRACKER_H__
#include <unordered_map>
namespace cige
{
namespace memory
{
class CIGE_API MemoryTracker
{
protected:
typedef struct AllocRecord
{
size_t bytes;
std::string filename;
size_t line;
std::string func;
AllocRecord() :
bytes(0), line(0)
{ }
AllocRecord(size_t sz, const char* file, size_t ln, const char* fun) :
bytes(sz), line(ln)
{
if (file)
filename = file;
if (fun)
func = fun;
}
} AllocRecord;
std::string m_leakFileName;
bool m_dumpToConsole;
typedef std::unordered_map<void*, AllocRecord> AllocMap;
AllocMap m_allocationMap;
size_t m_totalAllocations;
bool m_recordEnable;
protected:
void reportLeaks();
MemoryTracker() :
m_leakFileName("CIGEMemory.log"), m_dumpToConsole(true), m_totalAllocations(0), m_recordEnable(true)
{ }
public:
void setReportFileName(const std::string& name)
{
m_leakFileName = name;
}
const std::string& getReportFileName() const
{
return m_leakFileName;
}
void setReportToConsoleOutput(bool b)
{
m_dumpToConsole = b;
}
bool getReportToConsoleOutput() const
{
return m_dumpToConsole;
}
void setRecordEnable(bool b)
{
m_recordEnable = b;
}
bool getRecordEnable() const
{
return m_recordEnable;
}
size_t getTotalMemoryAllocated() const
{
return m_totalAllocations;
}
void _recordAlloc(void* ptr, size_t sz, const char* file = nullptr, size_t ln = 0, const char* fun = nullptr);
void _recordDealloc(void* ptr);
~MemoryTracker()
{
reportLeaks();
}
static MemoryTracker& get();
};
}
}
#endif // __MEMORY_TRACKER_H__
I'm getting: variable 'cige::memory::CIGE_API cige::memory::MemoryTracker' has initializer but incomplete type at the line with the class declaration. I've looked all over and I cant find any answers that have fixed this issue.
I'm also having the error expected '}' or ',' or ';' before 'protected' at the line with protected, right above the struct.
Any help with either of these two errors would be appreciated.
EDIT: CIGE_API is defined in a separate file (which is included), as __declspec(dllexport).
EDIT2: I fixed my problem (see the answer below). It was basically just Code::Blocks derping out pretty bad.
Looks like CIGE_API is not defined. So compiler try to resolve it like variable declaration class Type Variable {initializer-list}, where Type is CIGE_API and Variable is MemoryTracker.
In other words, syntactically you're predeclaring CIGE_API type and creating variable of this type instead of defining a class.
The definition
class CIGE_API MemoryTracker { ... };
is not valid C++. I guess CIGE_API is a macro defined to an implementation specific extension, but you didn't include the corresponding header which defines that macro.
Ok I ended up fixing my own problem. Code::Blocks wasn't properly finding files that were in my project (about the third time this has happened).
In entirely unrelated news, does anyone know another cross-platform IDE that works well for C++? (I already know about Eclipse).
I've encountered these two error when trying to compile..
anyone knows whats wrong ?
Was thinking maybe I #include the wrong header file ?
the sample of the codes and error as per following:
Error:
Square.cpp:8: error: redefinition of ‘Square::Square(bool, Point*, std::string, int)’
Square.h:21: error: ‘Square::Square(bool, Point*, std::string, int)’ previously defined here
Square.cpp: In member function ‘Point Square::getCoord()’:
Square.cpp:22: error: expected primary-expression before ‘]’ token
Square.cpp: In member function ‘void Square::setCoord(Point*)’:
Square.cpp:32: error: expected primary-expression before ‘]’ token
Square.cpp:32: error: expected primary-expression before ‘]’ token
cpp file
#include "Square.h"`
#include <cmath>
using namespace std;
Square::Square(bool containsWarpSpace, Point coord[], string shapeName, int vertPoint):ShapeTwoD(shapeName, containsWarpSpace) {
vertPoint = vertPoint;
coord[] = coord[];
}
int Square::getVertPoint()
{
return vertPoint;
}
Point Square::getCoord()
{
return coord[];
}
void Square::setVertPoint(int verticleP)
{
vertPoint = verticleP;
}
void Square::setCoord(Point coord[])
{
coord[] = coord[];
}
header:
#include "ShapeTwoD.h"
class Square : public ShapeTwoD
{
private:
int vertPoint;
Point coord[];
public:
//Accessor
int getVertPoint();
Point getCoord();
//Mutator
void setVertPoint(int vertP);
void setCoord(Point coord[]);
//virtual member
virtual double computeArea(Point x, Point y);
Square(bool containsWarpSpace, Point coord[], std::string shapeName = "Square", int vertPoint = 4):ShapeTwoD(shapeName, containsWarpSpace){}
};
You are defining the constructor twice, once in the header and once in the implementation file. In the header, you just need to declare it like this:
Square(bool containsWarpSpace,
Point coord[],
std::string shapeName = "Square",
int vertPoint = 4);
You also need to fix the handling of coord, maybe something like changing coord to
Point* coord;
and use
Point* Square::getCoord()
{
return coord;
}
and
this->coord = coord;
in the constructor and setCoord().
Please note that your way of handling coord seems strange and dangerous to me, but without further information about what you are actually trying to do it's hard to give specific advise. Generally, consider using the standard containers over manual memory/array management.
The compiler clearly tells you the problem:
You defined the constructor twice once in header file and once in cpp file.
Also, What exactly do you intend to do with:
coord[] = coord[];
You should understand each and every statement of code that you write. Think about, What do you intend this statement to do? & then match it to the language grammar that you learnt.
Source File:
Square::Square(bool containsWarpSpace, Point coord[],
string shapeName, int vertPoint)
:ShapeTwoD(shapeName, containsWarpSpace)
{
vertPoint = vertPoint;
coord[] = coord[];
}
Header File:
Square(bool containsWarpSpace, Point coord[],
std::string shapeName = "Square", int vertPoint = 4)
:ShapeTwoD(shapeName, containsWarpSpace)
{}
Looks like two different version of the same function.
The one in the header file calls the base class constructor but does not have any code in the body of the constructor.
I have recently started working with C++ classes and had just started when I reached an error. I have a "resource.h" file that contains the class definition of two classes: 'deck' and 'card'. I #included this file in another file, "card.cpp". In the card.cpp file I described all the methods/functions of the 'card' class. However on compilation I am getting the following the errors (fyi I am using the MinGW compiler for command-line):
card.cpp:3:29: error: ISO C++ forbids declaration of 'setCard' with no
type [-fp ermissive] card.cpp:3:1: error: prototype for 'int
Card::setCard(char, char)' does not matc h any in class 'Card'
resource.h:9:8: error: candidate is: void Card::setCard(char, char)
The "card.cpp" file:
#include "resource.h"
Card::setCard(char f, char s) {
face = f;
suit = s;
}
Card::Card (char face, char suit) {
setCard(face, suit);
}
Card::~Card () {}
The "resource.h" file:
typedef unsigned short int UINT;
class Card;
class Deck;
class Card {
public:
Card(char face, char suit);
~Card();
void setCard(char face, char suit);
char getFace() const { return face; }
char getSuit() const { return suit; }
private:
char face;
char suit;
};
class Deck {
public:
Deck();
~Deck();
Card getCard(UINT x);
private:
Card myCards[54];
};
What is causing this issue, and why in the world does the compiler think that "Card::setChard()" is an int
Card::setCard(char f, char s) {
face = f;
suit = s;
}
should be
void Card::setCard(char f, char s) {
face = f;
suit = s;
}
Some hints that helped me get to this amazing conclusion:
C++ forbids declaration of 'setCard' with no type
candidate is: void Card::setCard(char, char)
If you thought this was cryptic, hold on tight for when you get to templates. Compilers have a history of generating great error messages for them.