Template in Mac or Linux

I am practicing the follow code in Mac OS and Linux.

#include <iostream>
template<typename T>
class NamedObject {
public:
NamedObject(const char *name, const T& value);
NamedObject(const std::string& name, const T& value);

private:
std::string nameValue;
T objectValue_;

};


int main()
{
NamedObject<int> no1("Smallest Prime Number", 2);
NamedObject<int> no2(no1); //calls copy constructor
return 1;
}

I got the error message below.

Undefined symbols:
"NamedObject<int>::NamedObject(char const*, int const&)", referenced from:
_main in ccwOPv8q.o
ld: symbol(s) not found

I don't see any mistakes in my code.
Please help me why this error is coming.
Thank you
closed account (z05DSL3A)
You need to define your functions as well as declare them:
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
#include <iostream>

template<typename T>
class NamedObject 
{
public:
    NamedObject(const char *name, const T& value);
    NamedObject(const std::string& name, const T& value);

private:
    std::string nameValue;
    T objectValue_;

};


// Function Definitions
template<typename T>
NamedObject<typename T>::NamedObject(const char *name, const T& value)
    :nameValue(name), objectValue_(value)
{

}

template<typename T>
NamedObject<typename T>::NamedObject(const std::string& name, const T& value)
    :nameValue(name), objectValue_(value)
{

}


int main()
{
    NamedObject<int> no1("Smallest Prime Number", 2);
    NamedObject<int> no2(no1); //calls copy constructor
    return 1;
}
Last edited on
Thank you for the reply.
The code above still give the compilation error.

condes.cpp:17: error: template argument 1 is invalid
condes.cpp:17: error: declaration of template ‘template<class T> int NamedObject(const char*, const T&)’
condes.cpp:4: error: conflicts with previous declaration ‘template<class T> class NamedObject’
condes.cpp:4: error: previous non-function declaration ‘template<class T> class NamedObject’
condes.cpp:17: error: conflicts with function declaration ‘template<class T> int NamedObject(const char*, const T&)’
condes.cpp: In function ‘int NamedObject(const char*, const T&)’:
condes.cpp:18: error: only constructors take base initializers
condes.cpp: At global scope:
condes.cpp:24: error: template argument 1 is invalid
condes.cpp:24: error: declaration of template ‘template<class T> int NamedObject(const std::string&, const T&)’
condes.cpp:4: error: conflicts with previous declaration ‘template<class T> class NamedObject’
condes.cpp:4: error: previous non-function declaration ‘template<class T> class NamedObject’
condes.cpp:24: error: conflicts with function declaration ‘template<class T> int NamedObject(const std::string&, const T&)’
condes.cpp: In function ‘int NamedObject(const std::string&, const T&)’:
condes.cpp:25: error: only constructors take base initializers
closed account (z05DSL3A)
Sorry, it's been a long day it should have been:
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
#include <iostream>

template<typename T>
class NamedObject 
{
public:
    NamedObject(const char *name, const T& value);
    NamedObject(const std::string& name, const T& value);
	
private:
    std::string nameValue;
    T objectValue_;
	
};


	// Function Definitions
template<typename T>
NamedObject<T>::NamedObject(const char *name, const T& value)
:nameValue(name), objectValue_(value)
{
	
}

template<typename T>
NamedObject<T>::NamedObject(const std::string& name, const T& value)
:nameValue(name), objectValue_(value)
{
	
}


int main()
{
    NamedObject<int> no1("Smallest Prime Number", 2);
    NamedObject<int> no2(no1); //calls copy constructor
    return 1;
}
It's working.
Thank you for the help.
Last edited on
Topic archived. No new replies allowed.