Newsgroup: comp.lang.c++
Subject: Re: static const int problem
From: cathode26@...
Date: Sat, 27 Jul 2013 22:29:53 -0700 (PDT)
On Tuesday, May 15, 2007 6:17:01 AM UTC-5, 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<int> stripped(v.begin()+a, v.end());
> //std::vector<int> s2(v.begin()+m_static,v.end());
> }
> };
> int main()
> {
> C c;
> std::vector<int> pv;
> 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?
So I tried your code in Visual Studio and it works fine, but if you use http://codepad.org/ your code does not compile. It is true, this is a declaration and not a definition. You need to define it like so.
#include <vector>
class C {
private:
static const int m_static = 2;
public:
void f(const std::vector<int>& v);
};
const int C::m_static;
void C::f(const std::vector<int>& v)
{
int a = m_static;
std::vector<int> stripped(v.begin()+a, v.end());
std::vector<int> s2(v.begin()+m_static,v.end());
}
int main()
{
C c;
std::vector<int> pv;
int i;
pv.push_back(i);
pv.push_back(i);
pv.push_back(i);
c.f(pv);
}
Subject: Re: static const int problem
From: cathode26@...
Date: Sat, 27 Jul 2013 22:29:53 -0700 (PDT)
On Tuesday, May 15, 2007 6:17:01 AM UTC-5, 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<int> stripped(v.begin()+a, v.end());
> //std::vector<int> s2(v.begin()+m_static,v.end());
> }
> };
> int main()
> {
> C c;
> std::vector<int> pv;
> 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?
So I tried your code in Visual Studio and it works fine, but if you use http://codepad.org/ your code does not compile. It is true, this is a declaration and not a definition. You need to define it like so.
#include <vector>
class C {
private:
static const int m_static = 2;
public:
void f(const std::vector<int>& v);
};
const int C::m_static;
void C::f(const std::vector<int>& v)
{
int a = m_static;
std::vector<int> stripped(v.begin()+a, v.end());
std::vector<int> s2(v.begin()+m_static,v.end());
}
int main()
{
C c;
std::vector<int> pv;
int i;
pv.push_back(i);
pv.push_back(i);
pv.push_back(i);
c.f(pv);
}
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?52722-static-const-int-problem&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment