When I try compiling this:
#include "OriAudioCache.hpp"
int main()
{
System *audioSystem(0);
FMOD_RESULT result;
result = System_Create(&audioSystem);
FMOD_CHECK_STATE(result);
OriAudioCache cache(audioSystem, 20);
string title("Ambitious Girl");
string path("/home/findrzkeeprz/Desktop/Resources/The_Ambitious_Girl.mp3");
cache.LoadSound(title, path, Default);
vector<OriSound>::iterator v_iter(cache.FindSound(title));
cache.PlaySound(v_iter->sound());
}
Which uses these files:
OriAudioCache.hpp
#ifndef ORI_AUDIO_CACHE_HPP_
#define ORI_AUDIO_CACHE_HPP_
#include "OriSound.hpp"
#include "OriChannel.hpp"
class OriAudioCache
{
public:
OriAudioCache(System *audioSystem, int maxChannels);
~OriAudioCache()
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter)
{
v_iter->~OriSound();
}
delete audioSystem_;
}
void LoadSound(string const& title, string const& path, AudioLoadMode mode);
vector<OriSound>::iterator FindSound(string const& title);
void RemoveSound(string const& title);
void PlaySound(Sound* sound);
vector<OriChannel>::iterator RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode);
void ReleaseChannel(Channel *channel);
private:
void inline SortChannels() {sort(channels_.begin(),channels_.end());}
vector<OriSound> audioCache_;
vector<OriChannel> channels_;
System *audioSystem_;
};
#endif
and OriAudioCache.cpp
#include "OriAudioCache.hpp"
OriAudioCache::OriAudioCache(System *audioSystem, int maxChannels)
:audioSystem_(audioSystem), channels_(maxChannels){}
void OriAudioCache::LoadSound(string const& title, string const& path, AudioLoadMode mode)
{
OriSound sound(title, path, audioSystem_, mode);
vector<OriSound>::iterator pos =lower_bound(audioCache_.begin(), audioCache_.end(), sound);
audioCache_.insert(pos, sound);
}
vector<OriSound>::iterator OriAudioCache::FindSound(string const& title)
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
{
if(v_iter->title() == title) return v_iter;
else continue;
}
return audioCache_.end();
}
void OriAudioCache::RemoveSound(string const& title)
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
{
if(v_iter->title() == title) audioCache_.erase(v_iter);
else continue;
}
}
void OriAudioCache::PlaySound(Sound* sound)
{
bool channelAlloc(false);
FMOD_CHANNELINDEX allocMode = FMOD_CHANNEL_FREE;
vector<OriChannel>::iterator oriChannel = RequestChannel(channelAlloc, allocMode);
if(channelAlloc)
{
FMOD_RESULT result;
Channel *chnl = oriChannel->channel();
result = audioSystem_->playSound(allocMode, sound, false, &chnl);
FMOD_CHECK_STATE(result);
bool isPlaying(false);
chnl->isPlaying(&isPlaying);
while(isPlaying)
{
chnl->isPlaying(&isPlaying);
}
bool paused(false);
chnl->getPaused(&paused);
if(!paused)
{
ReleaseChannel(chnl);
}
SortChannels(); //sort channels, reoder for channel requests
}
}
vector<OriChannel>::iterator OriAudioCache::RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode)
{
vector<OriChannel>::iterator vOri_iter(channels_.begin());
if(vOri_iter->status() == false)
{
if(vOri_iter->channel() == 0)
{
allocMode = FMOD_CHANNEL_FREE;
vOri_iter->setStatus(true); // flag channel as being used
return vOri_iter;
}
else allocMode = FMOD_CHANNEL_REUSE;
vOri_iter->setStatus(true); // flag channel as being used
return vOri_iter;
}
else return channels_.end();
}
void OriAudioCache::ReleaseChannel(Channel *channel)
{
bool playing(false);
bool paused(false);
channel->isPlaying(&playing);
channel->getPaused(&paused);
if(!playing && !paused)
{
vector<OriChannel>::iterator vOri_iter(channels_.begin());
for(; vOri_iter != channels_.end(); ++vOri_iter)
{
if(vOri_iter->channel() == channel) vOri_iter->setStatus(false);
}
}
}
I get undefined reference errors:
findrzkeeprz#Aardvak:~/Documents/Chidori/Engine/Audio$ make
g++ -ggdb -I../../ -I../../Engine -I../../Include -I../../Public -o audio main.cpp ../../Libraries/FMODEX/libfmodex.so
/tmp/cctNhPVy.o: In function `main':
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:9: undefined reference to `OriAudioCache::OriAudioCache(FMOD::System*, int)'
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:12: undefined reference to `OriAudioCache::LoadSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, AudioLoadMode)'
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:13: undefined reference to `OriAudioCache::FindSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:14: undefined reference to `OriAudioCache::PlaySound(FMOD::Sound*)'
collect2: ld returned 1 exit status
make: *** [audio] Error 1
What I'm I doing wrong here?
You're compiling OriAudioCache.hpp, when you should be compiling OriAudioCache.cpp, that's assuming that file that contains the implementation.
In general, the term "Undefined reference" from a compiler (actually the linker) means that some code fragment is accessing a symbol that the linker could not find.
Common causes of these errors:
Source file containing the definition
is not included in the build process.
A source fragment (code lines) were
not removed.
A header file (class declaration)
declares the given method or symbol (which was not removed or needs to be implemented).
Incorrect or missing scope resolution
operator (namespace issue).
Build process using wrong version of
source files (e.g. file not checked
into CMS.).
Most cases of these errors are not about the C++ reference operator nor dereferencing pointers.
Solutions:
Code review by independent eye(s).
Code inspection tools: Cppcheck,
Valgrind, Klocwork, etc.
Make fewer changes, compiler more
often.
Use Test Driven Development. ;-)
Related
The following piece of test code is causing me trouble. It compiles ok and tests are passed, but when debugging and trying to step into function AddPizza in line (*) or (**) , it takes me to allocator.h to line allocator() throw() { } and then it continues below. So it doesn't take me inside the method to inspect if everything is all right. This doesn't happen for instance with the previous line with the method AddIngredient. What is going on, is there something wrong with my implementation of AddPizza or some other method that is causing this behavior?
By the way I am using Qtcreator on Windows 10.
TEST(TestPizzeria, TestPizza)
{
Pizzeria pizzeria;
try
{
pizzeria.AddIngredient("Tomato", "Red berry of the plant Solanum lycopersicum", 2);
pizzeria.AddIngredient("Mozzarella", "Traditionally southern Italian cheese", 3);
pizzeria.AddPizza("Margherita", vector<string> { "Tomato", "Mozzarella" });//(*)steping into it takes me to allocator.h to line `allocator() throw() { }` and then it continues below
pizzeria.AddPizza("Marinara", vector<string> { "Tomato" });
}
catch (const exception& exception)
{
FAIL();
}
try
{
pizzeria.AddPizza("Margherita", vector<string> { "Tomato", "Mozzarella" });// (**)also here
FAIL();
}
catch (const exception& exception)
{
EXPECT_THAT(std::string(exception.what()), Eq("Pizza already inserted"));
}
}
I report here both AddPizza and AddIngredient methods and all necessary methods just in case:
class Ingredient {
public:
string Name;
int Price;
string Description;};
class Pizza {
vector<Ingredient> ingredients;
public:
string Name;
void AddIngredient(const Ingredient& ingredient){ingredients.push_back(ingredient);}
};
class Pizzeria {
map<string, Ingredient> mapNameToIngredient;
map<string, Pizza> mapNameToPizza;
void AddPizza(const string &name, const vector<string> &ingredients)
{
if(mapNameToPizza.find(name) != mapNameToPizza.end())
{
throw runtime_error("Pizza already inserted");
}
else
{
Pizza pizza;
pizza.Name = name;
vector<string> ingredientss = ingredients;
for(vector<string>::iterator it = ingredientss.begin(); it != ingredientss.end(); it++)
{
Ingredient ingredient;
ingredient = FindIngredient(*it);
pizza.AddIngredient(ingredient);
}
mapNameToPizza[name] = pizza;
}
}
void AddIngredient(const string &name, const string &description, const int &price)
{
if(mapNameToIngredient.find(name) != mapNameToIngredient.end())
{
throw runtime_error("Ingredient already inserted");
}
else
{
Ingredient ingredient;
ingredient.Name = name;
ingredient.Price = price;
ingredient.Description = description;
mapNameToIngredient[name] = ingredient;
}
}
const Ingredient &FindIngredient(const string &name) const
{
auto it = mapNameToIngredient.find(name);
if(it != mapNameToIngredient.end())
{
return it->second;
}
else
{
throw runtime_error("Ingredient not found");
}
}
};
GTest use macros and global variables heavily, it's the logic of the GTest framework itself, if you try debugging with step-in, you will most likely enter the assist code other than your test code.
So it's recommended to add a breakpoint in the first line of TEST body, as #1201ProgramAlarm mentioned in the comment.
You can use g++ -E to see the code after preprocessing of the original c++ code:
#include <gtest/gtest.h>
TEST(foo, bar) { ASSERT_EQ(true, true); }
Even for this one-line test, we get one 73533 lines file after preprocessing. I have extracted the tail part and removed some file name and line number information, then we get code snippet as below(It may be different from yours since the compiler and GTest version may be different)
static_assert(sizeof("foo") > 1, "test_suite_name must not be empty");
static_assert(sizeof("bar") > 1, "test_name must not be empty");
class foo_bar_Test : public ::testing::Test {
public:
foo_bar_Test() {}
private:
virtual void TestBody();
static ::testing::TestInfo* const test_info_ __attribute__((unused));
foo_bar_Test(foo_bar_Test const&) = delete;
void operator=(foo_bar_Test const&) = delete;
};
::testing::TestInfo* const foo_bar_Test ::test_info_ =
::testing::internal::MakeAndRegisterTestInfo(
"foo", "bar", nullptr, nullptr,
::testing::internal::CodeLocation("a.cpp", 3),
(::testing::internal::GetTestTypeId()),
::testing::internal::SuiteApiResolver<
::testing::Test>::GetSetUpCaseOrSuite("a.cpp", 3),
::testing::internal::SuiteApiResolver<
::testing::Test>::GetTearDownCaseOrSuite("a.cpp", 3),
new ::testing::internal::TestFactoryImpl<foo_bar_Test>);
void foo_bar_Test ::TestBody() {
switch (0)
case 0:
default:
if (const ::testing::AssertionResult gtest_ar =
(::testing::internal::EqHelper::Compare("true", "true", true,
true)))
;
else
return ::testing::internal::AssertHelper(
::testing::TestPartResult::kFatalFailure, "a.cpp", 3,
gtest_ar.failure_message()) = ::testing::Message();
}
We can see that a class is defined by the TEST macro and a global variable is defined, so a direct step in may goes into GTest's internal code or generated code from GTest's macro.
I am having this issue in my cocoa pods file (leveldb-library), I don't know enough about C++ to be able to resolve it, however it appears to be a simple semantics error. I have just converted my Xcode project to Swift 5 and am attempting to enable Mac catalyst app support. I believe the other two issues are a result of the first issue and not independent issues.
The three error messages are namely;
ln 32: Out-of-line definition of 'TableCache' does not match any declaration in 'leveldb::TableCache'
ln 37: Reference to type 'const leveldb::Options' could not bind to an lvalue of type 'const leveldb::Options *'
ln 64: Indirection requires pointer operand ('const leveldb::Options' invalid)
Rightly or Wrongly I will include the whole file just incase the error lies elsewhere.
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "db/table_cache.h"
#include "db/filename.h"
#include "leveldb/env.h"
#include "leveldb/table.h"
#include "util/coding.h"
namespace leveldb {
struct TableAndFile {
RandomAccessFile* file;
Table* table;
};
static void DeleteEntry(const Slice& key, void* value) {
TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
delete tf->table;
delete tf->file;
delete tf;
}
static void UnrefEntry(void* arg1, void* arg2) {
Cache* cache = reinterpret_cast<Cache*>(arg1);
Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
cache->Release(h);
}
TableCache::TableCache(const std::string& dbname,
const Options* options,
int entries)
: env_(options->env),
dbname_(dbname),
options_(options),
cache_(NewLRUCache(entries)) {
}
TableCache::~TableCache() {
delete cache_;
}
Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
Cache::Handle** handle) {
Status s;
char buf[sizeof(file_number)];
EncodeFixed64(buf, file_number);
Slice key(buf, sizeof(buf));
*handle = cache_->Lookup(key);
if (*handle == NULL) {
std::string fname = TableFileName(dbname_, file_number);
RandomAccessFile* file = NULL;
Table* table = NULL;
s = env_->NewRandomAccessFile(fname, &file);
if (!s.ok()) {
std::string old_fname = SSTTableFileName(dbname_, file_number);
if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
s = Status::OK();
}
}
if (s.ok()) {
s = Table::Open(*options_, file, file_size, &table);
}
if (!s.ok()) {
assert(table == NULL);
delete file;
// We do not cache error results so that if the error is transient,
// or somebody repairs the file, we recover automatically.
} else {
TableAndFile* tf = new TableAndFile;
tf->file = file;
tf->table = table;
*handle = cache_->Insert(key, tf, 1, &DeleteEntry);
}
}
return s;
}
Iterator* TableCache::NewIterator(const ReadOptions& options,
uint64_t file_number,
uint64_t file_size,
Table** tableptr) {
if (tableptr != NULL) {
*tableptr = NULL;
}
Cache::Handle* handle = NULL;
Status s = FindTable(file_number, file_size, &handle);
if (!s.ok()) {
return NewErrorIterator(s);
}
Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
Iterator* result = table->NewIterator(options);
result->RegisterCleanup(&UnrefEntry, cache_, handle);
if (tableptr != NULL) {
*tableptr = table;
}
return result;
}
Status TableCache::Get(const ReadOptions& options,
uint64_t file_number,
uint64_t file_size,
const Slice& k,
void* arg,
void (*saver)(void*, const Slice&, const Slice&)) {
Cache::Handle* handle = NULL;
Status s = FindTable(file_number, file_size, &handle);
if (s.ok()) {
Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
s = t->InternalGet(options, k, arg, saver);
cache_->Release(handle);
}
return s;
}
void TableCache::Evict(uint64_t file_number) {
char buf[sizeof(file_number)];
EncodeFixed64(buf, file_number);
cache_->Erase(Slice(buf, sizeof(buf)));
}
} // namespace leveldb
Thank you for any help you would be able to provide.
EDIT: Here is (I believe) the requested header file.
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// Thread-safe (provides internal synchronization)
#ifndef STORAGE_LEVELDB_DB_TABLE_CACHE_H_
#define STORAGE_LEVELDB_DB_TABLE_CACHE_H_
#include <stdint.h>
#include <string>
#include "db/dbformat.h"
#include "leveldb/cache.h"
#include "leveldb/table.h"
#include "port/port.h"
namespace leveldb {
class Env;
class TableCache {
public:
TableCache(const std::string& dbname, const Options& options, int entries);
~TableCache();
// Return an iterator for the specified file number (the corresponding
// file length must be exactly "file_size" bytes). If "tableptr" is
// non-null, also sets "*tableptr" to point to the Table object
// underlying the returned iterator, or to nullptr if no Table object
// underlies the returned iterator. The returned "*tableptr" object is owned
// by the cache and should not be deleted, and is valid for as long as the
// returned iterator is live.
Iterator* NewIterator(const ReadOptions& options, uint64_t file_number,
uint64_t file_size, Table** tableptr = nullptr);
// If a seek to internal key "k" in specified file finds an entry,
// call (*handle_result)(arg, found_key, found_value).
Status Get(const ReadOptions& options, uint64_t file_number,
uint64_t file_size, const Slice& k, void* arg,
void (*handle_result)(void*, const Slice&, const Slice&));
// Evict any entry for the specified file number
void Evict(uint64_t file_number);
private:
Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle**);
Env* const env_;
const std::string dbname_;
const Options& options_;
Cache* cache_;
};
} // namespace leveldb
#endif // STORAGE_LEVELDB_DB_TABLE_CACHE_H_
I've writing a "big" Indexs project in C++, that is difficult for me meanwhile...
While i was trying to create an inheritance between 2 clases:
ZonalPermutant wich inherit from Permutant
i got the following error:
error:
g++ -Wall -std=c++0x lib/PermZone.o lib/VectorSpace.o lib/Vector.o lib/PermZoneMain.o lib/Permutant.o lib/ZonalPermutant.o -o permZone
lib/Permutant.o: In function `Permutant::Permutant()':
Permutant.cpp:(.text+0x20): undefined reference to `vtable for Permutant'
lib/Permutant.o: In function `Permutant::Permutant(long)':
Permutant.cpp:(.text+0x8e): undefined reference to `vtable for Permutant'
lib/Permutant.o: In function `Permutant::Permutant(PList<long>*, long)':
Permutant.cpp:(.text+0x10c): undefined reference to `vtable for Permutant'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant()':
ZonalPermutant.cpp:(.text+0x41): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant(long)':
ZonalPermutant.cpp:(.text+0xa4): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant(PList<long>*, PList<long>*, long)':
ZonalPermutant.cpp:(.text+0x13e): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::~ZonalPermutant()':
ZonalPermutant.cpp:(.text._ZN14ZonalPermutantD2Ev[_ZN14ZonalPermutantD5Ev]+0x2f): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o:(.rodata._ZTI14ZonalPermutant[_ZTI14ZonalPermutant]+0x10): undefined reference to `typeinfo for Permutant'
collect2: error: ld returned 1 exit status
Makefile:17: recipe for target 'permZone' failed
make: *** [permZone] Error 1
I know it is probably that the linking in my makefile could be wrong written. So i will show you my makefile:
makefile:
CC = g++
STD = -std=c++0x
DIR = -I .
CFLAGS = -Wall -c $(STD)
LFLAGS = -Wall $(STD)
BRUTEFORCE_LIB = lib/VectorSpace.o lib/Vector.o lib/BruteForce.o lib/BruteForceMain.o
PIVOT_LIB = lib/VectorSpace.o lib/Vector.o lib/Pivot.o lib/PivotMain.o
PERM_LIB = lib/Permutants.o lib/VectorSpace.o lib/Vector.o lib/PermMain.o lib/Permutant.o
BASICS_LIB = lib/MajorOrderHeap.o lib/MinorOrderHeap.o lib/PList.o lib/OList.o lib/PList.o lib/HeapElement.o lib/Random.o lib/Tokenizer.o lib/Matrix.o
PERMZONE_LIB = lib/PermZone.o lib/VectorSpace.o lib/Vector.o lib/PermZoneMain.o lib/Permutant.o lib/ZonalPermutant.o
default: permZone
#EXE's
#PermZone
permZone: $(PERMZONE_LIB)
$(CC) $(LFLAGS) $(PERMZONE_LIB) -o permZone
lib/PermZoneMain.o: src/PermZoneMain.cpp src/Index.h src/Space.h
$(CC) src/PermZoneMain.cpp $(CFLAGS) -o lib/PermZoneMain.o
lib/PermZone.o: src/Indexes/PermZone/PermZone.h src/Indexes/PermZone/PermZone.cpp $(BASICS_LIB)
$(CC) src/Indexes/PermZone/PermZone.cpp $(CFLAGS) -o lib/PermZone.o
lib/ZonalPermutant.o: src/Indexes/PermZone/ZonalPermutant.cpp src/Indexes/PermZone/ZonalPermutant.h lib/Permutant.o
$(CC) src/Indexes/PermZone/ZonalPermutant.cpp $(CFLAGS) -o lib/ZonalPermutant.o
lib/Permutant.o: src/Element.h src/Indexes/Permutants/Permutant.h src/Indexes/Permutants/Permutant.cpp $(BASICS_LIB)
$(CC) src/Indexes/Permutants/Permutant.cpp $(CFLAGS) -o lib/Permutant.o
and now the h and cpp files (i know its a lot of text):
Permutant.h:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "../../Element.h"
#include "../../Basics/PList.h"
#ifndef PERMUTANT_H
#define PERMUTANT_H
class Permutant : public Element
{
protected:
PList<long> permutation;//stores only ID's
public:
bool isInverted;
Permutant();
~Permutant();
Permutant(long id);
Permutant(PList<long>* permutation,long id);
void setPermutation(PList<long>* permutation);
PList<long> getPermutation();
void invertPermutation();
long distance(Permutant* other);
string toString();
static long spearmanFootRule(Permutant &p1, Permutant &p2);
};
#endif // PERMUTANT_H
Permutant.cpp:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "Permutant.h"
//I dont like this trick.. but i have to use it for future distance calculations
typedef long (*P_distance)(Permutant&,Permutant&);//i hope it doesn't cause problems with inheritance :D
P_distance p_distance;
Permutant::Permutant()
{
isInverted = false;
p_distance = &spearmanFootRule;
}
Permutant::Permutant(long id)
{
this->id = id;
isInverted = false;
p_distance = &spearmanFootRule;
}
Permutant::Permutant(PList<long>* permutation,long id)
{
this->id = id;
isInverted = false;
this->permutation = *permutation;
p_distance = &spearmanFootRule;
}
void Permutant::setPermutation(PList<long>* permutation)
{
this->permutation = *permutation;
}
PList<long> Permutant::getPermutation()
{
return this->permutation;
}
void Permutant::invertPermutation()
{
PList<long> *inverted_permutation = new PList<long>(permutation.size());
inverted_permutation->toArray();
for (long i = 0; i < permutation.size(); ++i)
{
(*inverted_permutation)[permutation[i]] = i;
}
this->setPermutation(inverted_permutation);
this->isInverted = !isInverted;
}
long Permutant::distance(Permutant* other)
{
return p_distance(*this, *other);
}
string Permutant::toString()
{
ostringstream oss;
oss << ((isInverted)?"i":"")<< " " << permutation.toString();
return oss.str();
}
long Permutant::spearmanFootRule(Permutant &p1, Permutant &p2)
{
long dist = 0;
if(p1.isInverted == p2.isInverted)
{
p1.invertPermutation();
}
for (int i = 0; i < p2.getPermutation().size(); ++i)
{
dist+= abs(p1.getPermutation().get(p2.getPermutation().get(i)) - i);
}
return dist;
}
ZonalPermutant:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "../Permutants/Permutant.h"
#ifndef ZONAL_PERMUTANT_H
#define ZONAL_PERMUTANT_H
class ZonalPermutant :public Permutant
{
private:
PList<long> zones;
public:
ZonalPermutant();
~ZonalPermutant(){};
ZonalPermutant(long id);
ZonalPermutant(PList<long>* permutation, PList<long>* zones, long id);
void setZones(PList<long>* zones);
PList<long> getZones();
long distance(Permutant* other);
string toString();
};
#endif // ZONAL_PERMUTANT_H
ZonalPermutant.cpp:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "ZonalPermutant.h"
ZonalPermutant::ZonalPermutant() : Permutant()
{}
ZonalPermutant::ZonalPermutant(long id) : Permutant(id)
{}
ZonalPermutant::ZonalPermutant(PList<long>* permutation, PList<long>* zones, long id) : Permutant(permutation,id)
{
this->zones = *zones;
}
void ZonalPermutant::setZones(PList<long>* zones)
{
this->zones = *zones;
}
PList<long> ZonalPermutant::getZones()
{
return this->zones;
}
long ZonalPermutant::distance(Permutant* other)
{
return 0;
}
string ZonalPermutant::toString()
{
return ":D";
}
Every of the other classes used in my code are good implemented and they dont create any problem at the moment of compiling and linking others Indexes...
If there is any problem in my code or way to programm, i would like that you say that to me :).
Thanks you for the help.
Try declaring your destructors virtual (usually no harm done if not necessary), and/or add their implementations to .cpp files. In my experience
Undefined reference to `vtable for Permutant'
hints to this direction.
In one of my methods, a function with two parameters is passed, and saved as rightClick. However, because its in a static function, the compiler wants the function to be initialised before. How can i go about this?
Mouse.cpp
void Mouse::clicked(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON) {
if(state == GLUT_DOWN) {
isDragging = true;
CurrentX = x;
CurrentY = y;
}
else
{
isDragging = false;
}
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
{
isDragging = true;
rightClick(x,y);
}
}
}
void Mouse::setRightClickFunction(void (*func)(int, int))
{
rightClick = func;
}
The setRightClickFunction is called before click ever is. Except now i'm getting a different problem : "Mouse::rightClick", referenced from:
Mouse::clicked(int, int, int, int) in Mouse.o
Based on your comments, you're getting a linker error about "undefined reference to Mouse::rightClick. This has nothing to do with function pointers. It's just that whenever you declare a static data member in a class, it's only a declaration. You have to define it somewhere (= in exactly one .cpp file).
Assuming your class Mouse looks something like this:
class Mouse
{
//...
static void (*rightClick)(int, int);
//...
};
You should put this line somewhere into Mouse.cpp:
void (*Mouse::rightClick)(int, int) = 0;
That will serve as the definition of the static data member rightClick.
Cocos2d-x is a C++ port of Cocos2d-for-iPhone. It has the advantage of cross-platform. I'm using Cocos2d-x to develop games for Android and iPhone.
Right now I'm compiling a set of Cocos2d-X code with both Android NDK and Xcode.
On Xcode the game compiles and runs well on the iPhone.
With Android NDK, the compile would fail. (I'm using the official Android r7c NDK).
Please help.
Edited: For those of you who're interested in the full implementation file. Here it is.
#include "GameOverScene.h"
#include "HelloWorldScene.h"
using namespace cocos2d;
bool GameOverScene::init() {
if (CCScene::init()) {
this->_layer = GameOverLayer::node();
this->_layer->retain();
this->addChild(_layer);
return true;
} else {
return false;
}
}
GameOverScene::~GameOverScene () {
if (_layer) {
_layer->release();
_layer = NULL;
}
}
bool GameOverLayer::init () {
if (CCLayerColor::initWithColor(ccc4f(255, 255, 255, 255))) {
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
this->_label = CCLabelTTF::labelWithString("", "Artial", 32);
_label->retain();
_label->setColor(ccc3(0, 0, 0));
_label->setPosition(ccp(winSize.width/2, winSize.height/2));
this->addChild(_label);
this->runAction(CCSequence::actions(CCDelayTime::actionWithDuration(3), CCCallFunc::actionWithTarget(this, callfunc_selector(GameOverLayer::gameOverDone)), NULL));
return true;
} else {
return false;
}
}
void GameOverLayer::gameOverDone() {
CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());
}
GameOverLayer::~GameOverLayer() {
if (_label) {
_label->release();
_label = NULL;
}
}
And the full header file
#ifndef S6_GameOverScene_h
#define S6_GameOverScene_h
#include "cocos2d.h"
class GameOverLayer : public cocos2d::CCLayerColor {
public:
GameOverLayer():_label(NULL) {};
virtual ~GameOverLayer();
bool init();
LAYER_NODE_FUNC(GameOverLayer);
void gameOverDone();
CC_SYNTHESIZE_READONLY(cocos2d::CCLabelTTF*, _label, Label);
};
class GameOverScene : public cocos2d::CCScene {
public:
GameOverScene():_layer(NULL) {};
~GameOverScene();
bool init();
//SCENE_NODE_FUNC(GameOverScene);
static GameOverScene* node()
{
GameOverScene *pRet = new GameOverScene();
//Error: undefined reference to `GameOverScene::init()'
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
else
{
//Error: undefined reference to `vtable for GameOverScene'
delete pRet;
pRet = NULL;
return NULL;
}
};
CC_SYNTHESIZE_READONLY(GameOverLayer*, _layer, Layer);
};
#endif
It might be problem with Android.mk file.. In that you need to add your GameOverScene.h file for compilation..
/Users/my_account_name/Desktop/Projects/S6/S6/android/jni/../../Classes/GameOverScene.h:40: undefined reference to GameOverScene::init()'
You have to link with GameOverScene's object file.
You might forget to add GameOverScene.cpp in Android.mk located at Classed folder.