C++ error: redefinition of class [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Note: I have searched thoroughly on SO and the solutions posted for other's with similar questions are not working for me here.
I am writing my own custom 'string-like' class in C++, and am encoutering the following errors when compiling:
./PyString.h:8:11: error: out-of-line declaration of 'PyString' does
not match any declaration in 'PyString' PyString::PyString (char*);
^
./PyString.h:9:11: error: definition of implicitly declared destructor PyString::~PyString (void);
pystring.cpp:4:7: error: redefinition of 'PyString' class PyString {
As for the first and second errors, moving around the destructor into the class definition itself in the cpp file did not work.
As for the third error, I can't seem to fix it - I'm not redefining the class!
Here is pystring.h:
#ifndef PYSTRING_INCLUDED
#define PYSTRING_INCLUDED
class PyString {
char* string;
};
PyString::PyString (char*);
PyString::~PyString (void);
#endif
Here is pystring.cpp:
#include "PyString.h"
#define NULL 0
class PyString {
char* string = NULL;
public:
PyString(char inString) {
string = new char[inString];
};
~PyString(void) {
delete string;
};
};
For reference, here is the compile output as a screenshot:
Any help is greatly appreciated.

You're defining your class PyString in your header AND in your cpp file, and also, a function definition doesn't need a ; at it's end.
And... your function prototypes needs to be in your class declaration in your header :
pystring.h
class PyString {
public: //ALWAYS indicate what is public/private/protected in your class
PyString (char* inString);
~PyString (); // Don't put void when there's no parameter
private: // All attributes are private
char* string;
};
pystring.cpp
#include "PyString.h"
PyString::PyString(char* inString) {
string = inString; // Avoid using new unless you're forced to
}
PyString::~PyString() {
}

Oh yes you are! pystring.h contains
class PyString {
char* string;
};
which is a class declaration. The declarations PyString::PyString (char*);
and PyString::~PyString (void); need to be inside that declaration.
But you have something similar in pystring.cpp, specifying additional functions, and defining some of them. That's what your compiler is telling you.
Normally, you fully define a class in a header (i.e. all members, and the declaration of the member functions), and implement the member functions of that class in a source file.
The moral of the story here: you can't really learn C++ by trial and error. Get a good book!

Related

Unable to access helper functions not in the namespace: undeclared identifier [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
I currently have a namespace set up like this:
SomeClass.h
namespace somenamespace {
class SomeClass {
public:
foo();
}
}
SomeClass.cpp
namespace somenamespace {
SomeClass::foo() {
somehelperfunction();
}
}
void somehelperfunction() {
std::cout << "hejflsdjf\n";
}
Without changing my header file, I cannot find a way to implement this helper function in a way which allows my class implementation to access the helper function. I was under the impression that as long as the helper function was in the same file I would be able to access it within the class implementation. But I get a "undeclared identifier" error when trying to build.
Functions must be declared before called.

(C++) How to call a variable name or get the value of a variable in different cpp files? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
enter image description here
if I want to call use the value of char slide_piece in another cpp file. How do i implement the header file and how to call the function? Thanks
GameLogic is a class and slide_piece should be a member of it. Than you have to add a getter function:
// GameLogic.h
class GameLogic
{
public:
char get_slide_piece() // getter function
{
return m_slide_piece;
}
// The rest of the class:
bool MouseDown( char file, char rank, wxPoint &point ); // Sets m_slide_piece value
// & more
protected:
char m_slide_piece = '\0';
// I use to add to member-variable names: "m_"
// So I suggest to rename it to m_slide_piece
// to make it easier to realize when you read the
// code in the cpp that it is a member-variable.
// The rest of the class
};
In the other cpp file, access it by:
char slide_piece = game_logic.get_slide_piece(); // game_logic is a GameLogic object
In your example, slide_piece is a variable of MouseDown function and it is not valid after the function returned. The use of global variables is not a good practice.
Starting with C++17, you can declare and define your constant in a header using inline:
inline constexpr char slide_piece = '\42';
Which is the easiest and cleanest solution for that problem IMHO.
Declare it in header file as extern:
MyHeader.h:
extern char slide_piece;
Define it in Source file: (including MyHeader.h is not necessary, but it's good practice)
MySource.cpp:
#include "MyHeader.h"
char slide_piece;
Use it in any other source by including the header:
OtherSource.cpp:
#include "MyHeader.h"
// slide_piece is available here

Problem with static data member - fixing linking error creates a compiler error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
game.h:
enum Game_state { MAIN_MENU, /*...*/ };
namespace list { class Linked_list { public: Linked_list() {} }; }
class Game {
public:
static Game_state state;
static list::Linked_list<Obj> objs;
};
Game_state Game::state = MAIN_MENU;
list::Linked_list<Obj> Game::objs = list::Linked_list<Obj>();
This gives me the linker error: multiple definition of Game::state (and Game::objs).
If I take out the type specifiers it gives me the compiler error: 'state' in 'class game' does not name a type (same for objs).
All I need is to initialize these members.
I'm using mingw on 32 bit windows 10.
You have to move those definitions into a translation unit (cpp file). Otherwise you will redefine them every time you include the header file somewhere, violating ODR.
Put the definitions of 'game::stat' and 'game::objs' in a *.cpp file and link against it.

c++ undefined reference to vtable exception [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've searched many answers but none of them can solve my problem, I'm new to c++, this issue is quite wired to me. Below is a simplified extraction of my code.
TestHeader.h:
#ifndef NAMESPACE_TESTHEADER_H_
#define NAMESPACE_TESTHEADER__H_
namespace Namespace {
class TestHeader {
public:
TestHeader(const std::string& str) : anyString_(str) { }
virtual std::string methodOne(const std::string& param) const;
virtual ~TestHeader() { anyString_.clear(); }
protected:
std::string anyString_;
};
}
#endif //NAMESPACE_TESTHEADER__H_
TestHeader.cpp:
#include "TestHeader.h"
using namespace std;
namespace Namespace {
TestHeader::TestHeader(const std::string& str):anyString_(str) { <do something>; }
std::string TestHeader::methodOne(const std::string& param) const
{
return <A string>;
}
TestHeader::~TestHeader() {
anyString_.clear();
}
}
What I did was simply call this line in any other .cpp in my package:
#include "TestHeader.h"
TestHeader testHeader("whatever");
The build failed by throwing
error: undefined reference to 'vtable for Namespace::TestHeader'
the vtable symbol may be undefined because the class is missing its key function
The most weird thing is: if I comment out virtual std::string methodOne(const std::string& str) const; in header and its implementation in .cpp, OR, if I comment out : anyString_(str) after constructor and anyString_.clear(); in destructor together in header only, the build will succeed.
Firstly You should not define the constructor and destructor twice. It shouldn't be compiling as mentioned by Curious in comments
Second I assume that you want don't the class to be abstract as there is no Runtime polymorphism implemented which is the basic use of Virtual functions.
If you don't want the class TestHeader to be abstract remove the virtual keyword which is referring to Virtual Table.C++ compiler inserts Virtual Table for every class having virtual function or class inherited from the class that has virtual functions.
Better study the use of Virtual keyword and then write the code.
Here are quick links for the same
Link 1
Link 2
Also, I think you need to revisit few concepts from Destructor virtual ~TestHeader() { anyString_.clear(); } does not make any sense. In fact, there is no base class which in turn denies the use of Virtual Destructor which is used in case of Inheritance
Firstly, include #include <string> at the top of your header file. I am guessing the error is because you have not linked the object file produced after compiling TestHeader.cpp with the source file that contains the declaration and initialization for the variable named testHeader
Compile these with the following command and you should see a linker error that complains saying that you have multiple definitions for the constructor
g++ -std=c++14 TestHeader.cpp yourfile.cpp
After you see those errors, remove the multiple definitions, either put all your definitions in the cpp file or only put them in one place and then recompile and link with the above command. The linker error should be gone.

no default constructor exists for class (c++ classes) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include<iostream>
#include<string>
using namespace std;
#ifndef TicTac_H
#define TicTac_H
class TicTac
{
public:
TicTac(int ,int);
void setpos(int);
void getpos(int);
void setpos2(int);
void getpos2(int);
bool takepos();
void setar(int&, int&);
void setarr();
void all(int,int);
void print();
int test();
private:
int p1;
int p2;
string tic[3][3] ;
string x;
string o;
int t1;
int t2;
bool ok;
};
#endif
**The compiler shown this message :
no default constructor exists for class "TicTac"
'TicTac' : no appropriate default constructor available
can anybody help me to fix this problem **
The error is surely not in that code, but in the code that includes that header and attempts to create an object of type TicTac without providing the two arguments that the constructor takes (two int). Other than that, the include guards should cover all the file (including the #include<...>) and you should never have a using directive (using namespace X) in a header.