Member reference base type is not a structure or union

Hi
I have a class reee that accepts as parameter a container.
This container could be a vector, a deque or another type.

Here the header file of the class:
1
2
3
4
5
6
7
8
9
10
11
template<typename IntContainer>
class reee
{
public:
    reee(IntContainer& ds);
    reee();
    void addone(int x);
    int val{};
    IntContainer vald;
    IntContainer add_val2(IntContainer a);
};


Here the cpp file of the class:
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
#include "reee.h"
template<typename IntContainer>
reee<IntContainer>::reee(IntContainer& ds)
{
    for(unsigned int i{};i<ds.size();i++)
    {
        cout<<"hi!"<<endl;
    }
}
template<typename IntContainer>
reee<IntContainer>::reee()
{

        cout<<"hi!"<<endl;

}
template<typename IntContainer>
void reee<IntContainer>::addone(int xs)
{
    val=+xs;
    cout<<"val:"<<val<<endl;
}

template<typename IntContainer>
IntContainer reee<IntContainer>::add_val2(IntContainer a)
{
    return vald=a;
}

And here the application in the main function:
1
2
3
4
5
        deque<int> cont1;
        cont1.push_back(5);
        reee<deque<int>> test1(deque<int> kkopp);
        test1->addone(4); //error
        test1.addone(4); //error 

As you can see, if you test it, I can not implement or use any function of "test1". The error is:
main.cpp:102:14: error: member reference base type 'reee<deque<int> > (deque<int>)' is not a structure or union

Can someone help? Thanks in advance.

Do you mean like this:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <deque>

template<typename IntContainer>
class reee {
public:
	reee(const IntContainer& ds);
	reee();
	void addone(int x);
	int val {};
	IntContainer vald;
	IntContainer add_val2(const IntContainer& a);
};

template<typename IntContainer>
reee<IntContainer>::reee(const IntContainer& ds)
{
	for (unsigned int i {}; i < ds.size(); i++)
		std::cout << "hi!\n";
}

template<typename IntContainer>
reee<IntContainer>::reee()
{
	std::cout << "hi!\n";
}

template<typename IntContainer>
void reee<IntContainer>::addone(int xs)
{
	val = +xs;
	std::cout << "val:" << val << '\n';
}

template<typename IntContainer>
IntContainer reee<IntContainer>::add_val2(const IntContainer& a)
{
	return vald = a;
}

int main()
{
	std::deque<int> cont1;
	cont1.push_back(5);

	auto test1 {reee(cont1)};

	test1.addone(4);
}

Hi!
Thanks for your help

On your line 46, If I write:
auto test1 {reee(cont1)};

It does not compile and says that
"use of class reee requires template arguments"

I corrected it to this:
1
2
3
4
5
6
7
8
std::deque<int> cont1;
            cont1.push_back(5);

            auto test1 {reee<deque<int>> (cont1)};
            test1.addone(4);
//I thought it may be the form how "test1" is declared, so I declared test2 as follows:
            reee<deque<int>>test2(cont1);
            test2.addone(3);

But now the compiler says
undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> > const&)'

undefined reference to `reee<std::deque<int, std::allocator<int> > >::addone(int)'

undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> > const&)'

undefined reference to `reee<std::deque<int, std::allocator<int> > >::addone(int)'


My code above compiles and runs OK with VS2019 as C++17.

What compiler/c++ version/os are you using?
I am using Qt. on the pro file it says i am using c++11. And I am on windows 10.

I also get this error in the end: :-1: Fehler: collect2.exe: error: ld returned 1 exit status
Last edited on
The compiler needs updating. The current standard is C++20 with C++17 as the previous standard.
new version is downloading. I will let you know when it finishes... Lets see if it is actually the compiler...
See below for an on-line version that compiles/runs OK.
https://wandbox.org/permlink/D0WsQ0fDeBRS2Bhg

Hi
I have c++ 17 now... For the people who do not know... The pro file is not the same as the standard that c++ has. What says in my pro file is still c++11, but my standard is c++ 17

I used this code to know it:
1
2
3
4
5
 if (__cplusplus == 201703L) std::cout << "C++17\n";
           else if (__cplusplus == 201402L) std::cout << "C++14\n";
           else if (__cplusplus == 201103L) std::cout << "C++11\n";
           else if (__cplusplus == 199711L) std::cout << "C++98\n";
           else std::cout << "pre-standard C++\n";

Nevertheless... I keep getting the same errors... It does not compile yet... I visited your link on wandbox, and I know it compiles there too... I have no idea why it does not on my program. Maybe it is because of other classes... Tomorrow I will check it out again. But no other classes are being used in the code, they are outcommented...
If someone understands what is happening I would be glad for the support.
You want this:
reee<std::deque<int>> test1 = reee<std::deque<int>>(cont1);

or even better, just
reee<std::deque<int>> test1(cont1);
Last edited on
I don't use Qt so can't specifically comment re that. However, most compilers (including VS2019) and the ones used for wandbox you have to specify the standard to be used. eg VS2019 defaults to C++14 unless instructed otherwise - even though the current version of VS2019 (16.10) supports C++20 if configured.

Does Qt have an option to specify the C++ standard to use for the compilation?
@OP
I have just run the @seeplus version of your code successfully on a mac using QCreator

untitled.pro file
1
2
3
4
5
6
7
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp


The terminal output is as follows and looks to be about what is to be expected albeit a fairly easygoing test.

hi!
val:4
Press <RETURN> to close this window...


It's the same +ve result whether its Qt 5.15.2 0r the latest Qt 6.1.0 so I doubt whether the C++ version is significant, even though I'm not using the Windows version of Qt.

Is your project setup as a "Non-Qt Project -> Plain C++ Application"?
Hello

I tried your code @Ganado.
I know it may seem weird what is happenning. The code has been written again without any other classes and the issue persists.

@againtry my project is set as console application, the proffessor gave us those instructions the first time we got used with the program.

The errors:
auto test1 {reee<deque<int>> (cont1)};
undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> >&)'
debug/main.o: In function `main':
undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> >&)'

test1.addone(4);
undefined reference to `reee<std::deque<int, std::allocator<int> > >::addone(int)'
reee<std::deque<int>> test3(cont1);
undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> >&)'
reee<deque<int>>test2(cont1);
undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> >&)'
test2.addone(3);
undefined reference to `reee<std::deque<int, std::allocator<int> > >::addone(int)'

:-1: Fehler: collect2.exe: error: ld returned 1 exit status
:-1: Fehler: [Makefile.Debug:71: debug/p4_tst2.exe] Error 1

Btw, I uninstalled and reinstalled Qt, issue persists... I compiled other projects I did before, without any problem.
Last edited on
@Mustermann, why don't you post the EXACT code that produced those errors - NOT little snippets of it. If you have divided your code into multiple files - and it looks like you have - post ALL those files, .h and .cpp.

Ideally, also give your compiler/linker command.
Hi @lastchance. I will post again all files. And all errors. I made a completely new project for this, without any other classes out commented:

.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef REEE_H
#define REEE_H

//#include "deque"
#include "iostream"
using namespace std;

template<typename IntContainer>
class reee
{
public:
    reee(const IntContainer& ds);
    reee();
    void addone(int x);
    int val{};
    IntContainer vald;
    IntContainer add_val2(const IntContainer& a);
};

#endif // REEE_H 


cpp.file:
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
#include "reee.h"
template<typename IntContainer>
reee<IntContainer>::reee(const IntContainer& ds)
{
    for(unsigned int i{};i<ds.size();i++)
    {
        cout<<"hi!"<<endl;
    }
}
template<typename IntContainer>
reee<IntContainer>::reee()
{
//    for(unsigned int i{};i<ds.size();i++)
    {
        cout<<"hi!"<<endl;
    }
}
template<typename IntContainer>
void reee<IntContainer>::addone(int xs)
{
    val=+xs;
    cout<<"val:"<<val<<endl;
}

template<typename IntContainer>
IntContainer reee<IntContainer>::add_val2(const IntContainer& a)
{
    return vald=a;
}

main file:
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
/*
 * Muster.cpp
 *
 */

#include "reee.h"
#include <deque>

int main()
{
    try
    {
        deque<int> cont1;
        cont1.push_back(5);
        auto test1 {reee (cont1)};
        test1.addone(4);

        reee<std::deque<int>> test3(cont1);
        reee<deque<int>>test2(cont1);
        test2.addone(3);

        return 0;

    }

    catch( std::exception& e ) {
        cerr << "Ausnahme: " << e.what()<<endl;
        return -3;
    }
    catch( ... ) {
        cerr << "Unbekannte Ausnahme"<<endl;
        return -1;
    }
}


Errors complete (with file path)(Fehler=error):

C:\Users\User\Documents\Pad2ss21\build-p4_tst2-Desktop_Qt_6_1_1_MinGW_64_bit-Debug\..\p4_tst2\main.cpp:17: Fehler: undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> > const&)'
debug/main.o: In function `main':
C:\Users\User\Documents\Pad2ss21\build-p4_tst2-Desktop_Qt_6_1_1_MinGW_64_bit-Debug/../p4_tst2/main.cpp:17: undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> > const&)'

C:\Users\User\Documents\Pad2ss21\build-p4_tst2-Desktop_Qt_6_1_1_MinGW_64_bit-Debug\..\p4_tst2\main.cpp:18: Fehler: undefined reference to `reee<std::deque<int, std::allocator<int> > >::addone(int)'
C:\Users\User\Documents\Pad2ss21\build-p4_tst2-Desktop_Qt_6_1_1_MinGW_64_bit-Debug\..\p4_tst2\main.cpp:20: Fehler: undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> > const&)'
C:\Users\User\Documents\Pad2ss21\build-p4_tst2-Desktop_Qt_6_1_1_MinGW_64_bit-Debug\..\p4_tst2\main.cpp:21: Fehler: undefined reference to `reee<std::deque<int, std::allocator<int> > >::reee(std::deque<int, std::allocator<int> > const&)'
C:\Users\User\Documents\Pad2ss21\build-p4_tst2-Desktop_Qt_6_1_1_MinGW_64_bit-Debug\..\p4_tst2\main.cpp:22: Fehler: undefined reference to `reee<std::deque<int, std::allocator<int> > >::addone(int)'
:-1: Fehler: collect2.exe: error: ld returned 1 exit status
:-1: Fehler: [Makefile.Debug:71: debug/p4_tst2.exe] Error 1

Errors are from line 15 to 20 without line 17 on the main file. It does not compile
Last edited on
Do not put templated functions into the cpp file. Since they are not implementations they are not found by the compiler/linker. So leave them in the header file.
@coder777 omg I do not know how to thank you... It works! Like, comment, subscribe... Finally after ONE WEEK oiashfioasfuioashfoiashaf.

Greetings and thanks again!
If you have a multi-file project (@seeplus's wasn't) then template functions need to go in header files, so that the correct functions will be created where required.

Just have the following TWO files (reee.hpp and main.cpp) and compile them with
g++ main.cpp
(or equivalent).


reee.hpp
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
35
36
37
38
39
40
41
42
43
44
45
46
#include "iostream"
using namespace std;    // <==== Needs to be fixed some time, since this is now a HEADER file


template<typename IntContainer>
class reee
{
public:
    reee(const IntContainer& ds);
    reee();
    void addone(int x);
    int val{};
    IntContainer vald;
    IntContainer add_val2(const IntContainer& a);
};


template<typename IntContainer>
reee<IntContainer>::reee(const IntContainer& ds)
{
    for(unsigned int i{};i<ds.size();i++)
    {
        cout<<"hi!"<<endl;
    }
}

template<typename IntContainer>
reee<IntContainer>::reee()
{
//    for(unsigned int i{};i<ds.size();i++)
    {
        cout<<"hi!"<<endl;
    }
}
template<typename IntContainer>
void reee<IntContainer>::addone(int xs)
{
    val=+xs;
    cout<<"val:"<<val<<endl;
}

template<typename IntContainer>
IntContainer reee<IntContainer>::add_val2(const IntContainer& a)
{
    return vald=a;
}




main.cpp
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
#include "reee.hpp"          // <===== Distinguish template-containing header file
#include <deque>
using namespace std;

int main()
{
    try
    {
        deque<int> cont1;
        cont1.push_back(5);
//      auto test1 {reee (cont1)};       // <==== Use of auto here is (IMO) asking for trouble
        reee<deque<int>> test1(cont1);   // <==== Be clearer
        test1.addone(4);

        reee<std::deque<int>> test3(cont1);
        reee<deque<int>>test2(cont1);
        test2.addone(3);

        return 0;

    }

    catch( std::exception& e ) {
        cerr << "Ausnahme: " << e.what()<<endl;
        return -3;
    }
    catch( ... ) {
        cerr << "Unbekannte Ausnahme"<<endl;
        return -1;
    }
}



Please note:
- using namespace std; will now be in a header file: you may want to amend that some time. It's up to you whether you have it in main.cpp (some parts of that currently require it).

- In my opinion, the overuse of auto can lead to some dangerous, current-standard-dependent ambiguities, especially in conjunction with {}. It has its place - and I use it sparingly - but I recommend only using it if you are absolutely certain what type would be deduced. TBH, "older" c++ standards (c++14 and below in this instance) adopt what, to me, would have been the more natural type-deduction here.
Last edited on
Topic archived. No new replies allowed.