Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this functor class :
#include <string>
using namespace std;
class IsPlayerOfType
{
public:
IsPlayerOfType(const string& type) : type_(type) {}
bool operator()(const Player* player) const
{
return (player->getType() == type_);
}
private:
string type_;
};
The class "Player" represent a player that has several methods and attributes. Among them, there is the method getType() which returns a string.
At some point of my program I have a variable called players_ which is of type vector<Player*>
Finally I have the following code to count the number of players of a certain type in my vector :
int number = count_if(players_.begin(), players_.end(), IsPlayerOfType("Defensive"));
When compiling I get a lot of errors such as :
error C2011: 'IsPlayerOfType' : 'class' type redefinition
error C2440: '' : cannot convert from 'const char [10]' to 'IsPlayerOfType'
error C2780: 'iterator_traits<_Iter>::difference_type std::count_if(_InIt,_InIt,_Pr)' : expects 3 arguments - 2 provided
I don't understand very well how count_if works, I tried to write this code inspiring myself from this answer : https://stackoverflow.com/a/13525420
I don't see where I'm wrong and the compiler errors confuse me.
My psychic debugging skills tell me that you forgot the #define include guards in the header that defines IsPlayerOfType causing the header to be multiply included in some source file. Keep in mind that #include works by the preprocessor doing text substitution which means the preprocessor would need additional logic to even attempt to prevent multiple inclusion.
Also note that using at file scope in a header is quite dangerous and should be avoided.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
insertItem (value_type )
{
}
Most code functions i have used so far have been along the lines of
insertItem( value_type var) Which allows me to access the data through var. In these new functions the only thing inside the parathesis is a object type. I cant access an object by saying only its type.
The functions in the header files do not use any variable names so i cant add to the functions in my implementation
No, you're wrong in saying the above statement. It is optional to name the parameters in the declarations(in header files) of the member functions. If you choose to not name the parameters in the declarations in header files, you can still name the parameter in the implementation files. One such example to get you started is shown below:
header.h
#pragma once
#include <string>
class Name
{
public:
//setter. Note here you can skip naming the parameter
void setName(std::string);
//constructor
Name(std::string); //note that here also we can skip naming the parameter
private:
std::string name;
};
source.cpp
#include "header.h"
//note here in implementation we've named the member function parameter to be p
void Name::setName(std::string p)
{
name = p;
}
//here in implementation we've named the constructor parameter to be p
Name::Name(std::string p): name(p)
{
}
main.cpp
#include <iostream>
#include<string>
#include "header.h"
int main()
{
Name n1("anoop");//this uses constructor
n1.setName("rana");//this uses the setter setName
return 0;
The output of the above program can be seen here
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 1 year ago.
Improve this question
I have been getting this Compiler/Linter Error every time I try to create a function member of a class with a return type other than int, double, and similar types. As far as I can tell, the compiler is setting the default int return type for these functions. But I can't figure out why. I have no excess header or cpp files in the directory, which I have also read can cause the problem.
The error is shows up under the addSample function.
error: no declaration matches 'int BinaryCounter::addSample(std::cxx11::string)' BinaryCounter::addSample(std::string sample)
Using these .cpp and .h files. Main is currently empty.
#include <vector>
#include <string>
class BinaryCounter
{
public:
BinaryCounter();
BinaryCounter(std::string sample);
~BinaryCounter();
std::vector<int> addSample(std::string sample);
std::vector<int> oneCountColumns;
int binaryLength;
};
#include "BinaryCounter.h"
BinaryCounter::BinaryCounter()
{
}
BinaryCounter::~BinaryCounter()
{
}
BinaryCounter::BinaryCounter(std::string sample)
{
binaryLength = sample.length();
}
BinaryCounter::addSample(std::string sample)
{
return oneCountColumns;
}
std::vector<int> addSample(std::string sample); returns an std::vector<int>, so at your cpp file you have to return the same type like this:
std::vector<int> BinaryCounter::addSample(std::string sample)
{
return oneCountColumns;
}
I was missing the std::vector in the .cpp file. This is what happens when I dust off the cobwebs to join AOC
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.
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 7 years ago.
Improve this question
I have two class: Clique and Graph,
when i try to create "Clique c" in the Graph class, VS give me two errors:
error C2065: 'Clique' : undeclared identifier
error C2065: 'c' : undeclared identifier
I tried to solve it for a long time and i'm stuck, pls help me.
this is my code:
Clique: http://pastebin.com/jw3FQv95
Graph: http://pastebin.com/bwLakmY0
thanks.
You have a circular inclusion, Graph.hpp includes Clique.hpp and Clique.hpp includes Graph.hpp.
Since to allocate a Clique in Graph class you need to know its full definition, you are forced to move the method that requires it in a separate source file (Graph.cpp) so that you will have something like
/* Clique.hpp */
#include "Graph.hpp"
class Clique { ... }
/* Graph.hpp */
// possibly a forward declaration to Clique here
class Graph {
...
Graph(const std::string &file, const double &th, const bool &debug, const bool &convert, const int &max_clique);
};
/* Graph.cpp */
#include "Clique.hpp"
Graph::Graph(const std::string &file, const double &th, const bool &debug, const bool &convert, const int &max_clique)
{
...
}
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