Unable to call constructor of friend class [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to call my SocketConnection constructor from inside the definition of the node class, but I'm failing to understand the compile error I'm getting - I fail to see why the compiler thinks the constructor I declared for SocketConnection is not a constructor.
Here are the main parts of .h file code:
class Node
{
public:
Node() ;
int OnStart() ;
friend class SocketConnection ;
} ;
class SocketConnection
{
public:
Node * m_nptr ;
int m_sockfd ;
SocketConnection(Node * nptr) ;
};
Here are the main parts of .cpp file:
int Node::OnStart()
{
SocketConnection newConnection(this) ;
return 0 ;
}
SocketConnection::SocketConection(Node * nptr): m_nptr(nptr)
{
}
On compilation, I get:
error: ISO C++ forbids declaration of ‘SocketConection’ with no type
error: no ‘int SocketConnection::SocketConection(Node*)’ member function declared in class ‘SocketConnection’
In member function ‘int SocketConnection::SocketConection(Node*)’:
error: only constructors take base initializers
Can someone help me understand this ?
Cheers,
N.

You have a typo:
SocketConnection::SocketConection(Node * nptr): m_nptr(nptr)
// ^
Change it into:
SocketConnection::SocketConnection(Node * nptr): m_nptr(nptr)
// ^^

Related

I'm trying to understand these errors in the code blocks [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 3 years ago.
Improve this question
Stack::Stack(const Stack& copy)
{
Stack temp;
copyHelper(copy.top, temp);
}
void Stack::copyHelper(Node* top, Stack newStack) {
if (top != nullptr)
{
copyHelper(top->getNext(), newStack);
newStack.push(top->getPayload());
}
}
I'm getting three errors related to the code block above:
Error C2600 'Stack::Stack': cannot define a compiler-generated special member function (must be declared in the class first) Program5 C:\Users\tcran\source\repos\Program5\Program5\Stack.cpp 14
Error C2264 'Stack::Stack': error in function definition or declaration; function not called Program5 C:\Users\tcran\source\repos\Program5\Program5\Stack.cpp 16
Error C2264 'Stack::Stack': error in function definition or declaration; function not called Program5 C:\Users\tcran\source\repos\Program5\Program5\Stack.cpp 23
Can someone please elaborate on why I am getting this errors?
class Stack
{
public:
// this constructor has not been specified in your class definition.
Stack(const Stack& copy);
};

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;
}

Why should I use a closing bracket in this? [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 7 years ago.
Improve this question
I have some code which I could not compile it yet. The compiler says there should be a closing bracket, but I can't see a reason for this or place to put it. This is my code:
#include "Player.h"
Player(std::string val){
set_Name(val);
set_Alliance("NONE");
set_LastUpdate();
}
Player(std::string val, std::string ally){
set_Name(val);
set_Alliance(ally);
set_LastUpdate();
}
I have included in Player.h
This is the error:
error: expected ')' before 'val'
This is the prototype for constructor:
Player(std::string);
I am using GNU GCC compiler, under linux(ubuntu)
You are missing the class name from constructor outside the class definition. Try this:
Player::Player(std::string val){ // constructor outside class definition
set_Name(val);
set_Alliance("NONE");
set_LastUpdate();
}
Unverified speculation: With your current code, compiler sees Player(symbol1 symbol2) and takes that as creating object of class Player, and first thing it fails to understand is seeing two symbols as constructor argument, and gives a somewhat misleading error about that.
When you define methods, constructor, destructor etc. outside of the class, remember to tell the compiler that this belongs to the class using the class name following the scope operator :: and the name of the method, constructor, destructor etc with the matching parameters.
As a small example:
class Phone {
string number;
public:
string get_num();
void set_num(string const &num) { number = num; }
};
// Pay attention to this:
// we tell the compiler that get_num belongs to class Phone
string Phone::get_num()
{
return number;
}
int main()
{
Phone p;
p.set_num("123");
cout << p.get_num() << endl;
}

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.

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

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" ;
}