C++: class "X" has no member named "Y" - c++

I have two projects: Project A & Project B.
In Project A, I have the class: 1.
I've included the class '1' from Project A using "#include '1' & using the configuration in eclipse.
When I try to access a public method from class 1 in Project B I'm getting the error:
Class "A" has no member named "Test".
What am I doing wrong???
EDIT: The class name & method is for skeleton purposes.
Project "MGeneral" has a class called "MGeneralCommands" -
class MGeneralCommands
{
public:
void sendCommand(TCPSocket * sock,int command);
void sendData(TCPSocket * sock,string data);
int readCommand(TCPSocket * sock);
string readData(TCPSocket * sock);
};
Project "MSA" has a class called "TCPMessengerServer" in which I’ve used "#include "MGeneralCommands.h"". For example, when trying to call the method "sendCommand" it won't recognize it.

From the comments, the error is actually something completely different:
‘sendData’ was not declared in this scope
from the code
sendData(socket,"TEST");
Unless you're already in a member function of MGeneralCommands (or a subclass), you'll need an object of that type to call it on:
commands.sendData(socket,"TEST");

Related

Cannot get override method in derived (googlemock-fixture) class with fallback to base fixture class to work

I'm fairly new to C++, more used to C and python, but google was not my friend this time.
I have a baseclass which implements some googlemock fixtures implemented in 2 generic classes (a baseclass and a parameterized class which derives from that baseclass) and a TEST_P function using the parameterized fixture.
Since the parameterized fixture is used to test 2 implementations I put the fixture-classes and the TEST_P in a baseclass cpp-file (+ hpp file) and the specific parameter-tables in 2 specific cpp files.
This works like a charm.
But... now we want to extend one of the implementations so that it needs a specific implementation of a function which is defined in the generic baseclass. For the other implementation the generic implementation is still correct.
So what I want is to override the generic implementation for only 1 of the 2 test-cpp files.
Initially (without override) I had the following in the hpp:
class MyBaseClass : public ::testing::Test
{
protected:
// boring fixture setup stuff
virtual void test( void );
}
class MyParametrizedBaseClass:
public MyBaseClass,
public::testing::WithParmInterface< std:tr1::tuple< /* parameter definition */ > >{
protected:
// some custom assert method definitions
}
baseclass.cpp file:
MyBaseClass::test( void )
{
cout << "This is the baseclass implementation" << std::endl;
}
...
TEST_P( MyParametrizedBaseClass, TestName )
{
test();
// other googlemock checks not needed for this question
}
And in the 2 specific implementations I have just the parameter tables:
std:tr1::tuple< /* parameter definition */ > const TableName[] = {
//table implementation
}
INSTANTIATE_TEST_CASE_P( Name, MyParameterizedBaseClass, ::testing::ValuesIn( TableName ) );
Again up to now this works.
Now for ... lets call it impl_1 ... we want test to do the following (this code does not work since the definition for MyParameterizedBaseClass::test is not found):
impl_1.cpp:
void MyParameterizedBaseClass::test( void )
{
cout << "This is the impl_1 implementation" << std::endl;
}
The first thing I did after seeing that the compiler misses the definition of MyParameterizedBaseClass::test was replace MyParameterizedBaseClass with MyBaseClass, but then it sees it as a redefinition (which it indeed is).
So next I googled and found the 'override' key word and adapted the MyParameterizedBaseClass definition so it looks like this:
class MyParametrizedBaseClass:
public MyBaseClass,
public::testing::WithParmInterface< std:tr1::tuple< /* parameter definition */ > >{
protected:
// some custom assert method definitions
void test( void ) override;
}
But when I do that I get the error:
Baseclass.hpp:[linenumber]: error: expected ';' before 'override'
And in the specific implementation for impl_1 it still says that no test function is declared in MyParameterizedBaseClass
And if I (knowing up front that it is wrong) put the ';' before 'override' as suggested, I get: error: ISO C++ forbids declaration of 'override' with no type
Next I removed the 'override' keyword and recompiled.
Now the compiler is satisfied, but the linker fails on impl_2 since there I don't have an implementation of MyParameterizedClass::test.
And that is true, since I want to have the default implementation of test for that.
(To complete the description: Yes it works again if I also implement test for MyParameterizedBaseClass in impl_2.cpp)
So my question is: what am I doing wrong?
Environment: Linux with GCC 4.4.2 (pretty old I know, but that is the version my company has)
Adding declaration in MyParametrizedBaseClass was a good move.
Problem is that you have used C++11 feature override and based on code like this:
std:tr1::tuple< /* parameter definition */ >
(tr1) indicates that you are using C++03 with "technical report one", so keyword override is unknown. That is why compiler complains: error: expected ';' before 'override'
So just remove override keyword and it should be fine.

How to declare a new class in the header and define it in the source file without this errors?

I'm on the situation where I want to create a new class and then use it in another created class (C++), but without using different header or source files: both classes shall be in the same place, one way or another. The main class shall contain only a pointer to the "child" class.
Now I know that in many cases is perfectly possible to define a class in the header file. In fact, if one wants to not just set a pointer to that "child class", but actually use one of its methods already in the header file (e.g., for inline methods), one would actually have to define it in the source file:
class ChildClass
{
public:
bool myFunctions() { return true; }
}
class MainClass
{
private:
ChildClass* poChildClass;
inline bool getResult() { return poChildClass->myFunctions(); }
}
But let's suppose I want just to have that pointer there, without any call to my ChildClass' methods, so I should be able to only declare the ChildClass and later define it in the same .cpp file as MainClass is defined:
//in .hpp
class ChildClass;
class MainClass
{
private:
ChildClass* poChildClass;
}
//in .cpp
class ChildClass
{
public:
bool myFunctions() { return true; }
}
//etc.
In a first moment I don't know what could be there of a problem. But in trying to do so with one of my classes in particular (which is based on Qwt's QwtPlotPicker class), I get some compile errors (in this last version):
error: undefined reference to `vtable for Picker'
The error points out where in the following code (in the .cpp):
class Picker : public QwtPlotPicker
{
Q_OBJECT
public:
Picker( QWidget* canvas ) :
QwtPlotPicker( canvas ) //Here lies the error the compiler says
//...
So what is the problem? What do I get this "undefined reference to 'vtable'" problem?
Thanks for any help,
Momergil
This is a problem I have had forever when using QT. Any class that has the Q_OBJECT macro in it MUST be listed in the HEADERS before running qmake (as far as I can tell). This may even mean putting the .cpp file in the HEADERS section.

Passing a pointer to template class as a parameter

I am having trouble passing a template class as a parameter to a another function.
I am using VS2012 c++/cli on a Windows 8.1 machine compiling for x64.
The compiler keeps telling me:
void Channel::TestFunc(SynchQueue<T> *)' : overloaded member function not found in 'Channel'
SynchQueue is a template class for a multi-threaded queue. I created it with another class I will call Images.
In my main.cpp, I have:
QPtr = new SynchQueue<Images>;
Also in main.cpp, I created a class called WorkerThread to which I passed QPtr.
No problems with that.
Now I want WorkerThread to pass QPtr to another class that is instantiated in WorkerThread.
So I defined the function as:
Channel.h
public ref class Channel
{
public:
// other definition stuff
void TestFunc(SynchQueue<Images> *tQPtr);
}
Channel.cpp
void Channel::TestFunc(SynchQueue<Images> *tQPtr)
{
int x;
x++;
}
I keep getting the error above. What am I doing wrong?
Any help appreciated.
You need:
template<typename T>
void TestFunc(SynchQueue<T> *tQPtr);

for internal method test case method is not accessible

I am writing unit test case for my internal method. I have made necessary changes in AssemblyInfo.cs of my mail class project
[assembly: InternalsVisibleTo("myunitest_assemblyname")]
now i can access my internal method in unit test case method.
but when i compile the code it is giving an error as below
Error 1 'main class project name' does not contain a definition for 'Process' and no extension method 'Process' accepting a first argument of type 'main class project name' could be found (are you missing a using directive or an assembly reference?)
my main class has strong name.
it would be great if some one point where i am missing.
main class structure
namespace Renewals
{
public class StateProcessor
{
internal virtual void PutEmailInQueue(DataTable dataTable)
{
}
}
}
//test class structure
namespace Renewals.Tests.Unit
{
[TestClass]
public class StateProcessorTest
{
[TestMethod]
public void PutEmailInQueueTest()
{
DateTime processingDate = Convert.ToDateTime("27-feb-2013"); ;
StateProcessor stateProcess = new StateProcessor(processingDate);
stateProcess.PutEmailInQueue(new DataTable());
}
}
}
PutEmailInQueue - this method giving me problem.
you wrote that your class use strong name.
I think you have to modify your InternalsVisibleTo() statement with the public key.
e.g.: [assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")]
for more informations see: http://msdn.microsoft.com/en-us/library/bb385180.aspx

Forward declaration issue

I have a cyclical redundancy circular dependency between two classes in my project, StatusEffect and BaseCharacter.
Both classes need to be aware of each other as the BaseCharacter needs to store a set of StatusEffects and StatusEffect needs to be able to do operations on BaseCharacter. I don't think it's possible to eliminate this behavior and still have it work correctly. Here's what I'm trying to do right now:
Base Character exists inside the namespace Game::Character and StatusEffect exists inside the namespace Game::StatusEffects
inside StatusEffects.h, I forward declared BaseCharacter like so:
namespace Game {
namespace Character {
class BaseCharacter;
}
}
then below it I have:
namespace Game
{
namespace StatusEffects
{
class StatusEffect
{
public:
virtual void TickCharacter(Game::Character::BaseCharacter* character, int ticks = 1)
{
std::cout << "Name " << character->GetName() << std::endl;
}
protected:
private:
std::string name;
int StatusEffectUID;
};
}
}
However, this is giving me a compiler error:
Error 1 error C2027: use of undefined type 'Game::Character::BaseCharacter'
I thought that because I'm using a pointer, this forward declaration is fine. Is there something else I need to forward declare? I dont need to forward declare the whole class definition do I?
You can't call a method through a pointer to a forward-declared class. You have to move this code somewhere where the class is already defined - for example into a .cpp file that includes both classes definition.
Your forward declaration is fine. However, you must not refer to any of the members of such a class.
You should only declare the TickCharacter method in the header. You should then define the StatusEffect::TickCharacter method in its own module file after #include ing the header file which contains the full declaration of BaseCharacter.