Google test fixture : passing a class member size [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I managed to successfully set up google test on my little program and creature a fixture class. I try to test the size of a vector of integer "start_ind_id_array_" public member of my class Genealogy.
Here is the code :
class BuildgenTest : public ::testing::Test {
protected:
virtual void SetUp(){
const string pedigree_fileName("../input_files/genealogies.txt");
const string start_filename("../input_files/start");
Genealogy curGen;
curGen.ReadPedigree(pedigree_fileName.c_str());
curGen.SetStartIndividual(start_filename.c_str());
cout << curGen.start_ind_id_array_.size() <<"\n" ;
}
Genealogy curGen;
};
TEST_F(BuildgenTest,veriftest){
int number_of_starting_individuals = curGen.start_ind_id_array_.size();
EXPECT_EQ(number_of_starting_individuals,3916);
}
The first cout gives me the number I expect, 3916. But then my test EXPECT_EQ fail because the size of my array is now 0, not my expected 3916.
Any idea what is causing this ?

You are shadowing curGen. You define it in setup function and in class body. The version in setup is shadowing the version in the body:
virtual void SetUp(){
const string pedigree_fileName("../input_files/genealogies.txt");
const string start_filename("../input_files/start");
Genealogy curGen;
^^^^^^^^^^^^^^^^^
curGen.ReadPedigree(pedigree_fileName.c_str());
curGen.SetStartIndividual(start_filename.c_str());
cout << curGen.start_ind_id_array_.size() <<"\n" ;
}

Related

Passing data between variables inside a class 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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I don't fully understand nor did i find anything online about this topic but this code(cut down so it wont be 500 lines of code):
class Character{
public:
int MaxHP;
int currentHP;
currentHP = MaxHP;
int getHP()
{
return CurrentHP;
}
Character(int h){
maxHP=h
};
~Character(){};
};
int main()
{
Character warrior(300)
cout<<getHP();
return 0;
};
maxHP = 300
CurrentHP = -875000
now trying to acces the value of currentHP i get -87878....
My question is what is the problem?
If you wish to initialize a private variable it's better done in a constructor:
class Character{
private:
int MaxHP;
int currentHP;
public:
Character() : MaxHP(0), currentHP(MaxHP){}
}

Cannot call member function, tried to do it properly still fails [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 4 years ago.
Improve this question
I implemented a new class (ProtoType) in my header file. Which looks like this:
class ProtoType : public Test
{
public:
uint32_t test();
};
class RealProtoType : public Real
{
public:
uint32_t real();
};
Then in C++ file I made this
uint32_t ProtoType::test()
{
return 5;
}
uint32_t RealProtoType::real()
{
uint32_t holder = ProtoType::test();
}
Then I get this error when compiling
error: cannot call member function ‘uint32_t ProtoType::test()’
without object uint32_t ProtoType::test();
But I still fail, how can I resolve this?
Since ProtoType::test() is a non-static member function you need an object of type ProtoType to call the function upon:
uint32_t RealProtoType::real()
{
ProtoType foo;
uint32_t holder = foo.test();
return 42;
}

What is the size of empty derived class in c++ and java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have got some related answers like Why the size of empty class that is derived from two empty classes is 2? but not get the answer to my question clearly.
interface PI1
{
default void show()
{
System.out.println("Default PI1");
}
}
interface PI2
{
default void show()
{
System.out.println("Default PI2");
}
}
class TestClass implements PI1, PI2
{
public void show()
{
PI1.super.show();
PI2.super.show();
}
public static void main(String args[])
{
TestClass d = new TestClass();
d.show();
}
}
Does this JAVA program show multiple inheritance?
In C++ the minimum size is 1.
However, the other question is about multiple inheritance from base classes of the same type. Two objects of the same type cannot have the same address, because then they would not be different objects.
The address is an important part of the identity of an object.
So, if you have two objects of the same type, the minimum size would be 2.
None of this happens in Java, because there is no multiple inheritance.

How to choose among two classes given a condition in c++? [closed]

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
Suppose there are two classes Class1 and Class2. given a condition I have to choose among them in shortest way possible without using if-else.
Means least lines of code.
At compile time only!!!
class class1{};
class class2{};
auto data = (((condition) ? class1 : class2) *)(variable)
Assuming you need to create object at compile time depending on a variable, you can try something like following
class class1{};
class class2{};
int main( int argc, char *argv[] )
{
constexpr bool variable =true;
/* x is object of type class1 or class2 depending on
compile time constant 'variable'
*/
typedef std::conditional<variable, class1, class2>::type x;
//std::cout << typeid(x).name() << '\n';
return 0;
}
See Here

What is wrong with my function? (method to copy from private class member into passed in float) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
These functions are both public members of a class. Private members of the class include *theCharArray and *theFloat.
This one works fine:
void theClass::getCharArray(char charArrayParam[]) const
{
strcpy(charArrayParam, this->theCharArray);
}
This one underlines "this" and VS express says "Error: Expression must be modifiable value"
void theClass::getFloat(float theFloatParam) const
{
theFloatParam = this->theFloat;
}
Please tell me what I'm doing wrong.
In theClass::getCharArray(char charArrayParam[]), charArrayParam is passed basically as a pointer to character array without any idea of the buffer size. This is kind of risky with the risk of overflowing the buffer. Netter interface would be:
theClass::getCharArray(char *charArrayParam, int charArraySize) const {
strncpy(charArrayParam, this->theCharArray, charArraySize - 1);
charArrayParam[charArraySize - 1] = 0;
}
And for the second one:
void theClass::getFloat(float *theFloatParam) const
{
*theFloatParam = this->theFloat;
}
otherwise, since theFloatParam being passed by value, changing that within the function has no effect on the caller.