Struct has not beed declared

Hey guys.
I'm currently having trouble accessing a struct from a templated base class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<std::size_t size>
class CBase
{
	struct Dummy_Test
	{
		float foo;
		std::array<uint8_t, size> array;
	};
};

template<std::size_t size>
class CDerived : public CBase<size>
{
	void Foo(Dummy_Test& test);
};


However this always gives me the error
error: 'Dummy_Test' has not been declared

I know i can fix this simply be writing
void Foo(typename CBase<size>::Dummy_Test& test);

However names could get pretty long. I was trying to shorten this be deriving from the base class.

Could you help me out here?
Last edited on
But then why not just declare the Dummy_Test struct outside of your CBase class template?
Since it appears your Dummy_Test struct does not depend on the template size parameter of CBase.

Fundamentally, part of the issue is that
CBase<2>::Dummy_Test and
CBase<3>::Dummy_Test
are different types, as far as the compiler cares.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Example program
#include <iostream>
#include <array>
#include <typeinfo>

template<std::size_t size>
class CBase
{
  public: // made public for demonstration
	struct Dummy_Test
	{
		float foo;
		std::array<uint8_t, 5> array;
	};
};

template<std::size_t size>
class CDerived : public CBase<size>
{
	//void Foo(Dummy_Test& test);
    void Foo(typename CBase<size>::Dummy_Test& test);
};

int main()
{
    if (typeid(CBase<2>::Dummy_Test) == typeid(CBase<3>::Dummy_Test))
    {
        std::cout << "same type\n";   
    }
    else
    {
        std::cout << "different types\n";   
    }
}


Edit:
For what it's worth, you can also do this:
1
2
3
4
5
6
7
8
template <std::size_t size>
using Dummy_Test = typename CBase<size>::Dummy_test;

template<std::size_t size>
class CDerived : public CBase<size>
{
	void Foo(Dummy_Test<size>& test);
};
Last edited on
Hey this was my error in this example. Of course std::array must depend on the size type

So
1
2
3
4
5
6
7
8
9
template<std::size_t size>
class CBase
{
	struct Dummy_Test
	{
		float foo;
		std::array<uint8_t, size> array;
	};
};


would be correct.
Your using solution works nicely.

How far is the scope of this using directive



1
2
3
4
5
6
7
8
9
10
11
12
13
template <std::size_t size>
using Dummy_Test = typename CBase<size>::Dummy_test;

template<std::size_t size>
class CDerived
{
	void Foo(Dummy_Test<size>& test);
};
class CDerived2
{
public:
	void Foo(Dummy_Test<2>& test);
}
;

It does not seem to be applied to CDerived2 because Dummy_Test<2> is not known.
So can i say that using xxx only applies to the next class or function and is not globally available?

Thx for your fast help
@JuliusCaeser

Several minor issues get in your way.

First, your example using declaration used Dummy_test, the the "T" is capitalized, Dummy_Test

Second, the struct is private (CBase is a class, the struct is declared in the default private section). Make it public.

You're missing a ";" after the CDerived2 class.

Then, it passes compilation.
ah thx very much that did help :)
I sometimes find it hard to spot my own mistakes :)
Topic archived. No new replies allowed.