| |

VerySource

 Forgot password?
 Register
Search
View: 1488|Reply: 10

Mentally retarded

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-21 09:20:02
| Show all posts |Read mode
I'm not a computer major student. I'm using C ++ recently and I'm fainted by a mentally handicapped include problem. Please master one or two:

The program has 3 files, main.cpp, a.h, a.cpp

The main function is in main.cpp
a.h is a header file of class a, which declares some interfaces
a.cpp is the implementation file for class a

code show as below:
//////////////////////////////////////main.cpp
#ifndef MAIN_CPP
#define MAIN_CPP

#include <iostream.h>
#include "a.h"

int main ()
{
  a <int> d;
  d.setdata (3);
  cout << d.getdata () << endl;
  return 0;
}

#endif

#ifndef A_H
#define A_H


/////////////////////////////////////////a.h
template <class type>
class a
{
public:
type data;

a () {}
~ a () {}

void setdata (type value);
type getdata ();
};

#endif

////////////////////////////////////////////////a.cpp
#ifndef A_CPP
#define A_CPP


#include "a.h"

template <class type>
void a <type> :: setdata (type value)
{
a <type> :: data = value;
}

template <class type>
type a <type> :: getdata ()
{
return a <type> :: data;
}

#endif


The question is:
This code can be passed when compiling, but there is a problem with the linking process. VC6 gives the following error
Compiling ...
main.cpp
Linking ...
main.obj: error LNK2001: unresolved external symbol "public: void __thiscall a <int> :: setdata (int)" (? setdata @? $ a @ H @@ QAEXH @ Z)
Debug / main.exe: fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

main.exe-2 error (s), 0 warning (s)


Then I copied the code in a.cpp to a.h, that is, a.h implements the declaration and definition of class a, and delete a.cpp at the same time, the program will have no problems!

why? ? ? ? ? ? ? ?
Reply

Use magic Report

1

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-31 09:54:02
| Show all posts
It seems a little nonsense, you include "a.h" connected program to a.h to find class a
include contains extern classes, i.e. external ones, of course wrong
No need to include directly class a
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-2-1 21:27:01
| Show all posts
You don't need to write this on template functions
template <class type>
type a :: getdata ()

Just like above

PS: Cpp files do not have to worry about being referenced ...
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-3 18:15:01
| Show all posts
Thank you both for your suggestions.
TO:fangtao826
What do you mean by “including class a without include”? Does it mean to define class a directly in main.cpp?
Now my question is: Why does using a.cpp produce link errors? How can I change it?

To:food0012
No need, I tried it myself and adopted
template <class type>
type a :: getdata ()
Generated a compilation error:
Compiling ...
a.cpp
C:\Documents and Settings\JINGLELONG\Desktop\test\a.cpp (13): error C2955: 'a': use of class template requires template argument list
        c:\documents and settings\jinglelong\desktop\test\a.h (15): see declaration of 'a'
Error executing cl.exe.

main.exe-1 error (s), 0 warning (s)
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-3 20:15:01
| Show all posts
I think this include error has nothing to do with the function template, it must be that the include in that place is wrong.
Reply

Use magic Report

0

Threads

24

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-17 10:30:01
| Show all posts
Few compilers currently support separation mode compilation

a.h a.cpp merge the contents into one file
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

 Author| Post time: 2020-3-23 01:45:01
| Show all posts
Thank you!
My compiler is VC6,

It is not possible to compile the console program separately, but it is possible in MFC.
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-24 21:00:01
| Show all posts
class a is a template, its name and definition are written together
Reply

Use magic Report

0

Threads

9

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-3 20:15:01
| Show all posts
#ifndef xxx
#define xxx
...
#endif

This structure is placed in the .h file (and it is quite necessary), it is not necessary to put this in the cpp file.
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-4 17:15:02
| Show all posts
This is the problem of the template class. Put the definition and implementation of the template in the .h file
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list