0

Vector2d.h:

#include <iostream>

using namespace std;

template <typename T, typename U>
class Vector2d
{
    T x;
    U y;
public:
    Vector2d (T X, U Y)
    {
        x = X;
        y = Y;
    }
    void printdata()
    {
        cout << "X: " << x << " Y: " << y;
    }

};

main.cpp:

#include <iostream>

#include "Vector2d.h"

using namespace std;
template <typename a, typename b>
int main()
{

    a Fnum;
    b Snum;
    cout << "Please enter two numbers ";
    cin << Fnum;
    cin << Snum;
    Vector2d<a, b> TestVector (a Fnum, b Snum);
    cout << TestVector.printdata();
    return 0;
}

My problem is I am trying to make a 2D vector class (for game design) and I wanted to try working with templates. I will probably want to just use floating point numbers in the end, but I still want to learn how to use templates. When I run this program I get a:

unresolved external symbol main referenced in function __tmainCRTStartup

and I'm not sure what I'm doing wrong. If you need me to elaborate please feel free to ask.

Nick Jarvis
  • 361
  • 1
  • 3
  • 9

5 Answers5

4

main() cannot be templated.

The C++ runtime will call into this function when your process starts. How is it to know what template parameters to provide?

Section 3.6.1.2 of the C++ standard (ISO/IEC 14882, second edition):

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

int main(int argc, char* argv[]) { /* ... */ }

Your definition of main does not fit either of these definitions, therefore C++ implementations are not required to support it, and I don't know of a single implementation that could do so reasonably, as it would have to provide its own type arguments and that doesn't make a whole lot of sense.

One of the purposes of templates is to allow you to write code that works with multiple different types, without having to write different code to support each type. This appears to be what you are trying to do. However, templates by themselves don't do anything -- they need to be instantiated, that is, used with real types at some point. An uninstatiated template is not even emitted as native code by the compiler, it is simply discarded!

All you have here are uninstantiated templates. At some point you need to specify actual types for template parameters, and you have not done so.

When the compiler compiles the file containing your main() the output object file doesn't contain a main() at all. This explains why the linker is unable to find it. (And even if you did explicitly instantiate it, the linker would be looking for a different symbol entirely!)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
3

main is a magic function called out specifically as the entry point of the program by the C++ specification. The signature must be either int main() or int main(int arc, char* arv[]), no templates. Your code does not have such a function, so the linker cannot link it into your executable.

Executable Programs themselves cannot be templated, so there's no way that even makes sense. When would you specifiy the parameter type of main? So you'll have to (A) use actual types in main, not templates, or (B), move the templated body to another function, and have main call that function, again specifying particular template parameter types.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
1

There are a couple of issues in your code. The first is that main cannot be a template. It is a very special function that needs to follow some rules.

Additionally, TestVector is not a variable but rather a function declaration. It declares a function that takes two objects of types a and b and returns Vector2d<a,b>.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

Also, cin uses this operator '>>'; not this one '<<'

cin >> Fnum;
cin >> Snum;
0

I think there are some problems in your code : the first the template must has a certain type before compiled,so you can use template before the main() the second "cin<