“内联”的问题 [英] problem with "inline"
问题描述
我做了以下测试程序:
////////////////////////classes_1.cpp >
#include< iostream>
#include" classes_1.h"
using namespace std;
A :: A():i(0){cout<<" const A:i =" << i<< endl;}
A ::〜A(){cout<<" destr A"<< endl;}
inline void A :: showA(){cout<< show A:i = <<我<< endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
A级{
int i;
public:
void set(int ii){i = ii;}
inline void showA();
A();
~A();};
#endif
/////////////////////// /
和一个简单的main.cpp制作一个A对象的文件,设置一个并显示
它。
编译没有问题,但我有以下链接错误:
- >错误LNK2001:未解析的外部符号" public:void __thiscall
A :: showA(void)" (?showA @ A @@ QAEXXZ)
当我删除两个内联时我没有链接错误。
我做错了什么?
提前致谢。
Hi, I made the following test program:
//////////////////////// classes_1.cpp
#include <iostream>
#include "classes_1.h"
using namespace std;
A::A():i(0){cout <<"const A: i =" << i <<endl;}
A::~A(){cout <<"destr A"<<endl;}
inline void A::showA() {cout << "show A: i =" << i <<endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
class A{
int i;
public:
void set(int ii){i=ii;}
inline void showA();
A();
~A();};
#endif
////////////////////////
and a simple "main.cpp" file which makes an A object , sets an i and shows
it.
It compiles without problems, but I have the following link-error:
->error LNK2001: unresolved external symbol "public: void __thiscall
A::showA(void)" (?showA@A@@QAEXXZ)
when I remove both the inlines I don''t have a link-error .
What am I doing wrong ??
Thanks in advance.
推荐答案
John Rambo写道:
John Rambo wrote:
////////////////////////classes_1.cpp
#include< iostream>
#include" classes_1.h"
使用命名空间std;
A :: A():i(0){cout<<" const A:i =" ; << i<< endl;}
A ::〜A(){cout<<" destr A"<< endl;}
内联void A :: showA(){ cout<< show A:i = <<我<< endl;};
//////////////////////// classes_1.h
#ifndef A_H > #define A_H
A类{
int i;
public:
void set(int ii){i = ii;}
inline void showA();
A();
~A();};
#endif
///////////////////// ///
和一个简单的main.cpp创建一个A对象的文件,设置一个并显示它。
它编译没有问题,但我有以下链接错误:
//////////////////////// classes_1.cpp
#include <iostream>
#include "classes_1.h"
using namespace std;
A::A():i(0){cout <<"const A: i =" << i <<endl;}
A::~A(){cout <<"destr A"<<endl;}
inline void A::showA() {cout << "show A: i =" << i <<endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
class A{
int i;
public:
void set(int ii){i=ii;}
inline void showA();
A();
~A();};
#endif
////////////////////////
and a simple "main.cpp" file which makes an A object , sets an i and shows
it.
It compiles without problems, but I have the following link-error:
[...]
将内联函数的实现从classes_1.cpp移到
classes_1.h
HTH,
Niels Dekker
www.xs4all.nl/~nd/dekkerware
谢谢,但是不可能将实现放在cpp中文件
(否则我的使用命名空间std有问题,这可能不是
放在头文件中......)???
Niels Dekker - 没有回复地址 <未***** @ this.is.invalid>在消息中写道
news:41 *************** @ this.is.invalid ...
Thanks, but isn''t it possible to place the implementation in the cpp file
(otherwise I have problems with the "using namespace std" which may not be
placed in the header file...) ???
"Niels Dekker - no reply address" <un*****@this.is.invalid> wrote in message
news:41***************@this.is.invalid...
John Rambo写道:
John Rambo wrote:
////////////////////////classes_1.cpp
#include< iostream>
#包括classes_1.h
使用命名空间std;
A :: A():i(0){cout<<" const A:i =" << i<< endl;}
A ::〜A(){cout<<" destr A"<< endl;}
内联void A :: showA(){ cout<< show A:i = <<我<< endl;};
//////////////////////// classes_1.h
#ifndef A_H > #define A_H
A类{
int i;
public:
void set(int ii){i = ii;}
inline void showA();
A();
~A();};
#endif
///////////////////// ///
和一个简单的main.cpp创建A对象的文件,设置i和
显示它。
它编译没有问题,但我有以下链接错误:
//////////////////////// classes_1.cpp
#include <iostream>
#include "classes_1.h"
using namespace std;
A::A():i(0){cout <<"const A: i =" << i <<endl;}
A::~A(){cout <<"destr A"<<endl;}
inline void A::showA() {cout << "show A: i =" << i <<endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
class A{
int i;
public:
void set(int ii){i=ii;}
inline void showA();
A();
~A();};
#endif
////////////////////////
and a simple "main.cpp" file which makes an A object , sets an i and shows it.
It compiles without problems, but I have the following link-error:
[。 ..]
将内联函数的实现从classes_1.cpp移到
classes_1.h
HTH,
Niels Dekker
www.xs4all.nl/~nd/dekkerware
John Rambo写道:
John Rambo wrote:
#include< iostream>
#include" classes_1。 h
使用命名空间std;
内联void A :: showA(){cout<< show A:i = <<我<< endl;};
我写道:将内联函数的实现从classes_1.cpp移到
classes_1.h
John Rambo写道:谢谢,但是不可能将实现放在cpp文件中
如果您希望您的成员函数是内联,则不是。
(否则我在使用命名空间std时遇到问题,这可能不会放在头文件中......)???
#include <iostream>
#include "classes_1.h"
using namespace std;
inline void A::showA() {cout << "show A: i =" << i <<endl;};
I wrote: Move the implementation of the inline function from classes_1.cpp to
classes_1.h
John Rambo wrote: Thanks, but isn''t it possible to place the implementation in the cpp file
Not if you want your member function to be "inline".
(otherwise I have problems with the "using namespace std" which may not be
placed in the header file...) ???
那是个很好的观点。但是你不需要使用命名空间std,如果
你做std ::!如下:
#include< iostream>
inline void A :: showA(){std :: cout<< show A:i = <<我<<
std :: endl;};
Niels Dekker
www.xs4all.nl/~nd/dekkerware
这篇关于“内联”的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!