c++


Forward declare a template class that needs to return to the original class?


How would I untangle this knot? I'm using Visual Studio as my compiler, if it matters. Obviously one could just move MOO to otherClass and move otherclass up, but this is a model for a more complex problem that's happening in reality. I think a forward definition is needed here, but I'm not sure how to structure it.
Can this knot even be untangled, or do I have to break sampClass into two classes, or a class with an interface?
#include <iostream>
class sampClass
{
public:
template <typename Z>
otherClass<Z> potato()
{
return otherClass<Z>(this);
}
sampClass(int a)
{
m_a = a;
}
void moo()
{
std::cout << "MOO!";
}
int m_a;
};
template <typename T>
class otherClass
{
public:
otherClass(sampClass* doMoo)
{
doMoo->moo();
}
};
int main()
{
sampClass a(5);
a.potato<int>();
return 0;
}
It's as simple as adding a forward declaration:
#include <iostream>
//
// forward declare otherClass here
//
template <typename T>
class otherClass;
class sampClass
{
public:
template <typename Z>
otherClass<Z> potato()
{
return otherClass<Z>(this);
}
sampClass(int a)
{
m_a = a;
}
void moo()
{
std::cout << "MOO!";
}
int m_a;
};
template <typename T>
class otherClass
{
public:
otherClass(sampClass* doMoo)
{
doMoo->moo();
}
};

Related Links

Compiler errors with cmath on Mac OS X
Array of fstream in C++
Error: (E107) bind interface to port failed: interface already bound to port: port 'MC8051_ALU().AM.port_27' (sc_in)
Error LINK2019 when I try to make a training for face detection [duplicate]
Quickest way to iterate in a C++ vector
How can I use snmp++ to enable/disable a switch port?
i wan't to use char* in this code instead of string how? [closed]
Using inRange() in OpenCV to detect colors in a range
The string.h 's function does not work in C++ but string does [on hold]
How to efficiently insert/merge into thrust::device_vector?
Change to transparent rectangle
Why addDays not working? [on hold]
OpenGL or GLFW renders ok only when moving the window
How to make static callback c++
How to copy host vector to device vector by thrust
conversion of string to const char*

Categories

HOME
audio
rx-java
composer-php
netlogo
text
google-speech-api
servo
c#-3.0
protege
flannel
boost-intrusive
treeset
sourceforge
relation
astyle
face-recognition
shogun
xamarin.mac
odbc
.net-4.5
sprite
build-automation
cpu-registers
robocopy
xbuild
mikrotik
indexoutofrangeexception
indexof
ebcdic
ddos
biblatex
jung
gperftools
easymock
hana-cloud-platform
webex
pivottable.js
dreamhost
xilinx-edk
azure-sql-server
android-optionsmenu
google-console-developer
imei
paypal-adaptive-payments
grunt-string-replace
qnx
http-status-code-415
react-d3
silent-installer
appharbor
matlab-guide
sp-executesql
invoke
oltp
jquery-ui-tabs
crypto++
lab-color-space
nuodb
docker-registry
erlang-shell
fixed
google-api-v3
qt4.8
stdio
mechanize
webtorrent
rational-team-concert
kendo-datepicker
google-roads-api
firefox-addon-bootstrap
pagerslidingtabstrip
supercsv
safari9
ncbi
cakedc
basic-msi
n-tier
bitmapimage
rails-i18n
iron
socketrocket
activator
inference
mat-file
filtered-lookup
facebook-events
jnetpcap
ipad-2
customer-support
appmobi
iphone-4
happstack
function-overloading
wcsf
xcode4.1
iphone-sdk-4.1
id-generation
open-basedir
mkstorekit
sqlmetal
trackback
remote-administration

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile