quick question... this is just a misunderstanding on inheritance my end so this should be quick to fix (I'm using objective-c++). I'll take the question off if it's already been solved... I couldn't find it hence my post:
I have the following:
Base.h file
#ifndef BASE_H
#define BASE_H
class Base {
public:
// Virtual Constructor
Base() {};
}
#endif
Derived.h file
// Derived.h
#ifndef DERIVED_H
#define DERIVED_H
#include "Base.h"
class Derived : public Base {
public:
Derived();
}
#endif
Derived.cpp file
// Derived.cpp
#include "Derived.h"
Derived::Derived() {
// Do construction here!
}
Wrapper.mm file
// Wrapper.mm
#include "Derived.h"
- (id)init {
if (self = [super init]) {
Base *b = new Derived(); // Cannot compile?
}
}
I'm getting a undefined error for "Derived::Derived()" referenced from -[Wrapper init] in Wrapper.o; Symbol(s) not found for architecture arm64. How do I fix this? Thanks!
You're not linking the .o file generated when compiling Derived.cpp. Just add Derived.o to the end of your compiler arguments when you compile your Wrapper.mm file to create an executable and you should be good (though I hadn't even ever heard of obj-c++ before)
Related
I want to create some objects that can delegate some work to its nested subobjects, but it pushes me into the circular dependency issues. Using of #ifndef directive works fine if I have only two classes (ClassA and ClassB), but this pattern doesn't work when ClassC is added. Is it possible to achieve such type of structure as shown in the code below and don't get an "undefined type" errors?
ClassA.h
#pragma once
#include "CoreMinimal.h"
#include "ClassB.h"
class UClassB;
#include ClassA.generated.h
UCLASS()
class PROJ_API UClassA : public UObject
{
GENERATED_BODY()
UPROPERTY()
UClassB* ObjB;
public:
void DoDelegation()
{
auto* ThisInstance = this;
ObjB = NewObject<UClassB>();
ObjB->DoWorkClassB(ThisInstance);
}
}
ClassB.h
#pragma once
#include "CoreMinimal.h"
//works nice
#ifndef CLASSA_H
#define CLASSA_H
#include "ClassA.h"
class UClassA;
#endif
//trying to use similar pattern which works with ClassA.h and ClassB.h
#include "ClassC.h"
class UClassC;
#include ClassB.generated.h
UCLASS()
class PROJ_API UClassB : public UObject
{
GENERATED_BODY()
UPROPERTY()
UClassC* ObjC;
public:
void DoDelegation()
{
auto* ThisInstance = this;
ObjC = NewObject<UClassC>();
ObjC->DoWorkClassC(ThisInstance);
}
void DoWorkClassB(UClassA* &ObjectClassA)
{
// do some stuff with ObjectClassA
}
}
ClassC.h
#pragma once
#include "CoreMinimal.h"
//trying to use similar pattern which works with ClassA.h and ClassB.h
//got "undefined type" error
#ifndef CLASSB_H
#define CLASSB_H
#include "ClassB.h"
class UClassB;
#endif
#include ClassC.generated.h
UCLASS()
class PROJ_API UClassC : public UObject
{
GENERATED_BODY()
public:
void DoWorkClassC(UClassB* &ObjectClassB)
{
// do some stuff with ObjectClassB
}
}
creating classes that refer to each other
Is it possible to achieve such type of structure as shown in the code below and don't get an "undefined type" errors?
Certainly. Referring to an object (with a pointer for example) of some class only requires declaration of the other class, not definition.
Simple solution is to declare both classes before defining either of them.
It's still not fully clear for me, but at least i understood why forward declaration wasn't worked for the first time. Inline implementation of methods that do something with such referencing classes is strictly not recommended. It compiles well If function is implemented in cpp.
I'm very new to C++, and I'm trying to use googlemock and googletest. I can't seem to get them to work even in simple examples so I'm sure there must be something simple I'm missing. I would really appreciate some help on this. I have a class Foo which has another class Bar as a dependency so I'm trying to mock this dependency out in tests. I'm sorry in advance, this seems like the simplest Dependency Injection example to me but it still spreads across 9 files! This is Bar:
// lib/bar.h
#ifndef BAR_H
#define BAR_H
class Bar {
public:
Bar(int baz);
virtual ~Bar() {};
int _baz;
};
#endif
With implementation:
// lib/bar.cpp
#include "bar.h"
Bar::Bar(int baz) : _baz(baz) {}
Here is the MockBar header:
// tests/mock_bar.h
#ifndef MOCK_BAR_H
#define MOCK_BAR_H
#include <gmock/gmock.h>
#include "../lib/bar.h"
class MockBar : public Bar {
public:
MockBar();
virtual ~MockBar() {};
};
#endif
And the implementation:
// tests/mock_bar.cpp
#include "mock_bar.h"
MockBar::MockBar() : Bar(0) {
}
So MockBar is just Bar with baz set to 0. Here is Foo:
// lib/foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class Foo {
public:
Foo(Bar bar);
virtual ~Foo() {};
Bar _bar;
int getBaz();
};
#endif
// lib/foo.cpp
#include "foo.h"
Foo::Foo(Bar bar) : _bar(bar) {
}
int Foo::getBaz() {
return _bar._baz;
}
And here is my test:
// tests/test_foo.h
#ifndef TEST_FOO_H
#define TEST_FOO_H
#include <gtest/gtest.h>
#include "../lib/foo.h"
#include "mock_bar.h"
class FooTest : public ::testing::Test {
public:
FooTest();
Foo subject;
MockBar mock_bar;
virtual ~FooTest() {};
};
#endif
// tests/test_foo.cpp
#include "test_foo.h"
FooTest::FooTest() : mock_bar(), subject(mock_bar) {
}
TEST_F(FooTest, BazTest)
{
ASSERT_TRUE(subject.getBaz() == 0);
}
Finally, the main test function is:
// tests/main.cpp
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "test_foo.h"
int main(int argc, char **argv) {
testing::InitGoogleMock(&argc, argv);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
When I compile this all together with:
g++ tests/main.cpp tests/test_*.cpp tests/mock_*.cpp lib/*.cpp -o test
-lgtest -lpthread -std=c++11
I get the error:
Undefined symbols for architecture x86_64:
"testing::InitGoogleMock(int*, char**)", referenced from:
_main in main-0b53fe.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
I would really appreciate some help, please let me know if I can be more clear!
You are using parts from gmock but only linking gtest. Thats why InitGoogleMock() is undefined.
Replacing -lgtest with -lgmock should make it.
The reason: gtest is testing framework, gmock the mocking framework.
If you link gmock, its includes also gtest but not the other way round.
all! I am trying to create a very simple inheritance structure using C++ and header files but (of course) I am having some difficulties.
When I try to compile my main program, I get this error:
In function `Base::Base()':
undefined reference to 'vtable for Base'
In function `Derived::Derived()':
undefined reference to 'vtable for Derived'
All I want is to print is
printed in Derived
but I am having some extreme difficulties.
Here are my program files:
main.cpp
#include <iostream>
#include "Base.h"
#include "Derived.h"
using namespace std;
int main(void) {
Base *bp = new Derived;
bp->show();
return 0;
}
Base.cpp
#include <iostream>
#include "Base.h"
virtual void Base::show() {
cout << "printed in Base";
}
Base.h
#ifndef BASE_H
#define BASE_H
class Base {
public:
virtual void show();
};
#endif
Derived.cpp
#include <iostream>
#include "Derived.h"
using namespace std;
void Derived::show() override {
cout << "printed in Derived";
}
Derived.h
#ifndef DERIVED_H
#define DERIVED_H
class Derived: public Base {
public:
void show() override;
};
#endif
Thank you! And any help is very much appreciated!...extremely.
As pointed out in the comments, by calling g++ main.cpp you are only compiling main.cpp.
You need to compile all files, then link them together. If you do so, you will see that there are compilation issues in your other cpp files as also pointed out in the comments (virtual and override only belong in the header).
So you need to call the following to compile all files:
g++ main.cpp Base.cpp Derived.cpp -o myapp
I face an issue when I build and link my code with GHS multi compiler.
This is roughly the idea:
base.h -->
#ifndef base_h
#define base_h
class Base
{
void basefncn1(); // defined in src file
void basefncn2(); // defined in src file
void basefncn3(); // defined in src file
}
#endif
interface.h -->
#ifndef interface_h
#define interface_h
#include "base.h"
class Interface : public Base
{
void basefncn1();
}
#endif
derivedclass.h -->
#ifndef derived_h
#define derived_h
#include "base.h"
#include "interface.h"
class Derived : public Interface
{
void basefncn1();
}
#endif
The linker error I get is:
basefncn2() and basefncn3() is multiply defined -> Defined both in base.o and derived.o.
The header files are guarded.
Am I doing anything wrong?
Edit: I tried changing the interface.h file. The function is now defined in interface.cpp.
So basically, interface.h and derived.h does not have functions defined in it.
I have a test class, it has nothing in it apart from the bones of a class.
h:
#ifndef TEST_H_
#define TEST_H_
class Test
{
public:
Test();
};
#endif /* TEST_H_ */
cpp
#include "test.h"
Test::Test()
{
}
And then in my main class I have:
Test *test = new Test();
I also include test.h.
I get the error:
Undefined reference to Test::Test()
Can anyone tell me where i'm going wrong?
Figured it out, even after cleaning the project and restarting the IDE it still wouldn't work. I had to manually add in test.h and test.cpp to my config.pri