C++ “在课外声明xxx不是定义”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16465573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Declaration of xxx outside of class is not definition" error
提问by M171
This is the error:
这是错误:
error: declaration of 'DataStream::DataStream()' outside of class is not definition [ fpermissive]|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
This is the main.cpp file:
这是 main.cpp 文件:
#include <iostream>
#include <iomanip>
#include "DataStream.h"
#include "MsgPacket.h"
using namespace std;
DataStream * Packet = new DataStream();
DataStream::DataStream();
int main() {
int source;
int destination;
int type;
int port;
int input;
std::string data;
cout << "My Assignment" << endl;;
MsgPacket * Packet = new MsgPacket(source,destination,type,port,data);
}
}
This is the MsgPacket.h
这是 MsgPacket.h
#ifndef MSGPACKET_H
#define MSGPACKET_H
#include <string>
#include "PacketAddress.h"
using namespace std;
class MsgPacket : public PacketAddress {
public:
MsgPacket();
MsgPacket (const MsgPacket & rhs);
MsgPacket(string dataIn);
MsgPacket(int source, int destination, int port, int type, std::string data);
MsgPacket(int ,char data);
string toString();
string getData() const {return _data;};
void setData(string inData) {_data = inData;};
string dataOutput();
virtual ~MsgPacket();
virtual MsgPacket * Clone() { return new MsgPacket(*this); }
protected:
string _data;
};
#endif // MSGPACKET_H
And finally this is the MsgPacket.cpp
最后这是 MsgPacket.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "msgpacket.h"
using namespace std;
MsgPacket::MsgPacket():
PacketAddress(0,0)
{
}
MsgPacket::MsgPacket (const MsgPacket & rhs):
PacketAddress(rhs),
_data(rhs.getData())
{
}
MsgPacket::MsgPacket(string dataIn):
PacketAddress(0,0){
string temp;
temp = dataIn.substr (0,4);
_source = atoi(temp.c_str());
temp = dataIn.substr (5,4);
_dest = atoi(temp.c_str());
temp = dataIn.substr (10,4);
_type = atoi(temp.c_str());
temp = dataIn.substr (15,4);
_port = atoi(temp.c_str());
_data = dataIn.substr (20,dataIn.length());
#ifdef DEBUG
cout << "CREATE PACKET: " << this->toString() << endl;
#endif
}
MsgPacket::MsgPacket(int source, int destination):
PacketAddress(source,destination)
{
}
MsgPacket::MsgPacket(int source, int destination, int port):
PacketAddress(source,destination)
{
_port = port;
}
MsgPacket::MsgPacket(int source, int destination, int type, int port, std::string data):
PacketAddress(source, destination)
{
_source = source;
_dest = destination;
_type = type;
_data = data;
_port = port;
}
string MsgPacket::dataOutput()
{
stringstream output;//create a stringstream
output << setw(4) << setfill('0') << _source << ":" << setw(4) << setfill('0') << _dest << ":" << setw(4) << setfill('0') << _type << ":" << setw(4) << setfill('0') << _port << ":" << _data;
return output.str();
}
string MsgPacket::toString()
{
stringstream output;//create a stringstream
output << "[" << showbase << hex << this << "] S:[" << _source << "] D:[" << _dest << "] P:[" << _type << "] T:[" << _port << "]" << " DATA[" << _data << "]";
return output.str();
}
回答by dshepherd
Another way you can end up with this error is something like the following code.
另一种可能会导致此错误的方法类似于以下代码。
class A
{
void a_function();
};
A::a_function(); // Note the semicolon here
{
// function contents...
}
The extra semicolon can be quite hard to spot if you haven't had enough caffeine yet...
如果您还没有摄入足够的咖啡因,则很难发现多余的分号...
回答by user327407
In case somebody gets here because of syntax error, please note that you can have the same error reported with a syntax error at initializing the parameters:
如果有人因为语法错误而到达这里,请注意您可能会在初始化参数时报告相同的错误和语法错误:
class A
{
A ();
char b;
};
A::A ()
, b(0) // wrong, should be ':' instead of ','
{
}
回答by Alok Save
DataStream::DataStream();
Is a declaration of constructor for class DataStream
, it must be declared within the class not outside.
是 class 的构造函数声明DataStream
,它必须在类内部而不是外部声明。
class DataStream
{
public:
DataStream();
};
Further, You can define this constructor inline inside the class or outside, like
此外,您可以在类内部或外部内联定义此构造函数,例如
DataStream::DataStream()
{}
回答by phonetagger
Yet another way I managed to get this error, by defining a static std::vector
of objects whose type was defined internal to the class:
我设法得到这个错误的另一种方法是定义 a static std::vector
of 类型在类内部定义的对象:
template<typename Ty>
class MyTemplatedClass
{
public:
struct AStructThatBelongsToMyTemplatedClass
{
uint32_t aU32ThatsPartOfTheStructThatBelongsToMyTemplatedClass;
};
static uint32_t u32;
static AStructThatBelongsToMyTemplatedClass aStructInstance;
static std::vector<uint32_t> vectOfu32;
static std::vector<AStructThatBelongsToMyTemplatedClass> vectOfStructInstances;
};
template<typename Ty> uint32_t MyTemplatedClass<Ty>::u32;
template<typename Ty> std::vector<uint32_t> MyTemplatedClass<Ty>::vectOfu32;
// This produces error: need 'typename' before 'MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass' because 'MyTemplatedClass<Ty>' is a dependent scope
//template<typename Ty> MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass MyTemplatedClass<Ty>::aStructInstance;
// ...but this works ok:
template<typename Ty> typename MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass MyTemplatedClass<Ty>::aStructInstance;
// This produces error: declaration of 'std::vector<MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass> MyTemplatedClass<Ty>::vectOfStructInstances' outside of class is not definition
//template<typename Ty> std::vector<MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass> MyTemplatedClass<Ty>::vectOfStructInstances;
// ...but this works ok (the only thing I added was 'typename'):
template<typename Ty> std::vector<typename MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass> MyTemplatedClass<Ty>::vectOfStructInstances;