Redefinition of a class in C++ [closed] - c++

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 8 years ago.
Improve this question
I'm new to C++ and stuck in a problem with an Error, called
"Semantic Issue: Redefinition of 'B' cB.h".
I have two classes, A and B, where cA should handle an Object of cB by reference and one friend function of cA, fExample. This is what the code looks like:
.h file cA:
#include "cB.h"
class A{
int val1, val2;
public:
friend void fExample(int, cB &);
};
.h file cB:
class B{
int val1, val2;
public:
void set_val1(int);
};
.cpp file cB:
#include <iostream>
#include "cB.h"
using namespace std;
void B::set_val1(int tVal){
val1 = tVal;
}
For me, it seems there is no way of working with the cB-object by reference with a friend function of cA. I would know some workarounds, but that's not my intention, I want to learn how to handle this problem the right way.
So thanks in advance for helping!

This type of error often happen due to missing include guards. The Simplest way is:
#ifndef HEADER_NAME
#define HEADER_NAME
You may also use #pragma once

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.

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.

I receive this "error: 'string' not declared" when compiling [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am running into an error when compiling. The error states that 'string' is not declared. I know I'm not supposed to "include" anything in these files. The header file declares a class, and lists the member functions. Two of those functions have an integer passed as a parameter, and two others have a string passed as a parameter. The .cpp file is using namespace, and the string variable is definitely declared. What could be the issue?
Security.h
class Security
{private:
public:
void Driver();
void EncFileUsingRot(int rotNum);
void EncFileUsingCrypto(string file);
void DecFileUsingRot(int rotNum);
void DecFileUsingCrypto(string file);
};
Security.cpp
#include<fstream>
#include<iostream>
#include "Security.h"
using namespace std;
Two errors.
Firstly Security.h should have
#include <string>
Secondly
void EncFileUsingCrypto(string file);
should be
void EncFileUsingCrypto(std::string file);
and
void DecFileUsingCrypto(string file);
should be
void DecFileUsingCrypto(std::string file);

Should headers used as part of a base class interface be included in the derived class [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Take the following example:
// base.h
#include <string>
struct base
{
virtual ~base() = default;
virtual void do_something(const std::string& arg) const = 0;
};
// derived.h
struct derived : base
{
void do_something(const std::string& arg) const
{
//...
}
};
In this example should derived.h include the string header?
I fully agree with the principle of including what you use but in this case base.h has to include string and if the interface changes to not use string (and the include <string> is accordingly removed from base.h) then the interface will break highlighting anyways.
If the interface changes not to include string, your derived do_something will have to change as well. If you need in the derived.h independently of the interface (e.g. due to details unrelated to interface but present in the implementation) - in this case yes, it's valid and probably preferable to include <string> there as well.
I take the approach of always includeing anything I need for that file. I then don't have to go around hunting through headers and untangling webs of dependencies when I'm missing a symbol somewhere. Include guards mean that this is cheap.
But it's completely up to you. If you want to omit it, to make derived.cpp shorter, and add it in at a later date if the need arises, that's fine too.
There's simply no "right" answer.
You can include "base.h". Don't think too much.The less code you write,the better your code is.
#include "base.h"
struct derived :public base
{
void do_something(const std::string& arg) const
{
//...
}
};

Problem using recursive method in c++ [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 have a class, defined in a .h like this
#ifndef JLLABOUR_H
#define JLLABOUR_H
class JLLabour{
public:
JLLabour(int, int);
double* recursivefft(double*,int);
void FFT(int*);
~JLLabour();
private:
int width;
int height;
};
#endif // JLLABOUR_H
and in my .cpp I have the definition of my recursive function, the problem is that when I call it again , during compilation it doesnt allow me to continue because the method has not been defined yet. I dont know how to solve this, please help.
#include <JLLabour.h>
double* JLLabour::recursivefft(double* x,int asize){
//operations and declartions...
//...
even = recursiveFFT(sum,m); //<-- the problem is here, in the recursion.
odd = recursiveFFT(diff,m);
// more operations....
return result;
}
}
FYI I am compiling under Linux, using Qt because Im developing a graphic app...
C++ is case sensitive. Your method is called recursivefft not recursiveFFT.