static const int问题 [英] static const int problem

查看:99
本文介绍了static const int问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




以下代码有效:


#include< vector>

class C {

私人:

static const int m_static = 2;

public:

void f(const std :: vector< int>& v)

{

int a = m_static;

std :: vector< intstripped(v.begin() + a,v.end());

//std::vector<ints2(v.begin()+m_static,v.end());

}

};

int main()

{

C c;

std :: vector< intpv;

int i;

pv.push_back(i);

pv.push_back(i);

pv.push_back(i);

cf(pv);

}

但是当我删除评论时在void f(...)中,编译器给出了一个

错误:


g ++ -ansi -Wall -o test test.cpp
/tmp/cckLnGUY.o:在函数`C :: f(std :: vector< int,std :: allocator< int>


const&)'':



test.cpp :(。text._ZN1C1fERKSt6vectorIiSaIiEE [C :: f(std :: vector< int,

std :: allocator< int const&)] + 0xdb):未定义的引用` C :: m_static''

collect2:ld返回1退出状态

make:*** [test]错误1


g ++ --version

g ++(GCC)4.1.2 20060928(预发布)(Ubuntu 4.1.1-13ubuntu5)

有谁能告诉我我在做什么错了?

解决方案

mati写道:




以下代码有效:


#include< vector>

class C {

private:

static const int m_static = 2;

public:

void f(const std :: vector< int>& v)

{

int a = m_static;

std :: vector< intstripped(v.begin()+ a,v.end ());

//std::vector<ints2(v.begin()+m_static,v.end());

}

};

int main()

{

C c;

std :: vector< intpv;

int i;

pv.push_back(i);

pv.push_back(i);

pv.push_back(i);

cf(pv);

}


但是当我删除评论时void f(...),然后编译器给出一个

错误:


g ++ -ansi -Wall -o test test.cpp

/tmp/cckLnGUY.o:在函数`C :: f(std :: vector< int,std :: allocator< int>


const& )'':



test.cpp :(。text._ZN1C1fERKSt6vectorIiSaIiEE [C :: f(std :: vector< int,

) std :: allocator< int const&)] + 0xdb):未定义引用`C :: m_static''

co llect2:ld返回1退出状态

make:*** [test]错误1


g ++ - version

g ++ (海湾合作委员会)4.1.2 20060928(预发行)(Ubuntu 4.1.1-13ubuntu5)


有谁能告诉我我做错了什么?



即使你在类声明中执行初始化,你仍然需要在cpp文件上定义静态成员变量。

奇怪的是,即使没有它,visual studio也会编译代码。


问候,


Zeppe


即使你在类声明中执行初始化,你


仍然需要定义cpp文件上的静态成员变量。

奇怪的是,即使没有它,visual studio也会编译代码。


问候,


Zeppe



嗯。我刚刚测试了代码: http://dinkumware.com/exam/Default.aspx

在VC8,EDG和MINGW上,每个编译器都会生成可执行文件。也许 -

ansi开关正在使用此错误?!


2007年5月15日星期二13:17:01 +0200 ,mati写道:





以下代码有效:



嗯...没有:


main.cpp:在成员函数中a ?? void C :: f(const std :: vector< int,std :: allocator< ; int&)a ??:

main.cpp:10:错误:a ?? s2a ??未在此范围内声明


#include< vector>

class C {

private:

static const int m_static = 2;

public:

void f(const std :: vector< int>& v)

{

int a = m_static;

std :: vector< intstripped(v.begin()+ a,v.end()); // std :: vector< int>

s2(v.begin()+ m_static,v.end());

};



什么是s2? ......啊,如果你删除了//然后突然间它是std :: vector< int ...并且所有编译都没问题。


-

Lionel B


Hi

The following code works:

#include <vector>
class C {
private:
static const int m_static = 2;
public:
void f(const std::vector<int>& v)
{
int a = m_static;
std::vector<intstripped(v.begin()+a, v.end());
//std::vector<ints2(v.begin()+m_static,v.end());
}
};
int main()
{
C c;
std::vector<intpv;
int i;
pv.push_back(i);
pv.push_back(i);
pv.push_back(i);
c.f(pv);
}
But when I erase the comment in the void f(...), then compiler gives an
error:

g++ -ansi -Wall -o test test.cpp
/tmp/cckLnGUY.o: In function `C::f(std::vector<int, std::allocator<int>

const&)'':

test.cpp:(.text._ZN1C1fERKSt6vectorIiSaIiEE[C::f(std::vector<int,
std::allocator<int const&)]+0xdb): undefined reference to `C::m_static''
collect2: ld returned 1 exit status
make: *** [test] Error 1

g++ --version
g++ (GCC) 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
Can anybody tell me what I''m doing wrong?

解决方案

mati wrote:

Hi

The following code works:

#include <vector>
class C {
private:
static const int m_static = 2;
public:
void f(const std::vector<int>& v)
{
int a = m_static;
std::vector<intstripped(v.begin()+a, v.end());
//std::vector<ints2(v.begin()+m_static,v.end());
}
};
int main()
{
C c;
std::vector<intpv;
int i;
pv.push_back(i);
pv.push_back(i);
pv.push_back(i);
c.f(pv);
}
But when I erase the comment in the void f(...), then compiler gives an
error:

g++ -ansi -Wall -o test test.cpp
/tmp/cckLnGUY.o: In function `C::f(std::vector<int, std::allocator<int>

const&)'':

test.cpp:(.text._ZN1C1fERKSt6vectorIiSaIiEE[C::f(std::vector<int,
std::allocator<int const&)]+0xdb): undefined reference to `C::m_static''
collect2: ld returned 1 exit status
make: *** [test] Error 1

g++ --version
g++ (GCC) 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
Can anybody tell me what I''m doing wrong?

Even if you perform the initialization inside the class declaration, you
still need the definition of the static member variable on a cpp file.
Strangely enough, visual studio compiles the code even without it.

Regards,

Zeppe


Even if you perform the initialization inside the class declaration, you

still need the definition of the static member variable on a cpp file.
Strangely enough, visual studio compiles the code even without it.

Regards,

Zeppe

Hmmm. I just tested code at: http://dinkumware.com/exam/Default.aspx
on VC8, EDG and MINGW and every compiler make executable files. Maybe -
ansi switch is using this error ?!


On Tue, 15 May 2007 13:17:01 +0200, mati wrote:

Hi

The following code works:

Um... no:

main.cpp: In member function a??void C::f(const std::vector<int, std::allocator<int&)a??:
main.cpp:10: error: a??s2a?? was not declared in this scope

#include <vector>
class C {
private:
static const int m_static = 2;
public:
void f(const std::vector<int>& v)
{
int a = m_static;
std::vector<intstripped(v.begin()+a, v.end()); //std::vector<int>
s2(v.begin()+m_static,v.end());
};

What''s s2 ? ... ah, if you remove the "//" then suddenly it''s a std::vector<int... and all compiles ok.

--
Lionel B


这篇关于static const int问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆