Vector size is not correct - how can we initialize a vector inside a structure
I have below sample code:
#include <iostream>
#include <vector>
typedef struct defs
{
std::vector<int> myvec;
} dvec;
typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Compile and execute:
$ c++ -std=c++11 try58.cpp
$ ./a.exe
my vec size is 0
I was expecting the size of vector to be 1 - why the same is 0?
c++ c++11
|
show 2 more comments
I have below sample code:
#include <iostream>
#include <vector>
typedef struct defs
{
std::vector<int> myvec;
} dvec;
typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Compile and execute:
$ c++ -std=c++11 try58.cpp
$ ./a.exe
my vec size is 0
I was expecting the size of vector to be 1 - why the same is 0?
c++ c++11
8
What's up with thosetypedef
's everywhere? You don't need them
– Rakete1111
Nov 12 at 14:59
5
Such confusion, much wow
– Zereges
Nov 12 at 15:01
4
@Programmer That's a style used in c becausestruct
doesn't define a type name there. In c++ you don't need totypedef
them sincestruct
create type names.
– François Andrieux
Nov 12 at 15:01
2
dvec1.myvec.push_back(10);
pushes data to a different vector, than you are trying to reference inv1.myvec.size()
.
– Algirdas Preidžius
Nov 12 at 15:02
1
I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05
|
show 2 more comments
I have below sample code:
#include <iostream>
#include <vector>
typedef struct defs
{
std::vector<int> myvec;
} dvec;
typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Compile and execute:
$ c++ -std=c++11 try58.cpp
$ ./a.exe
my vec size is 0
I was expecting the size of vector to be 1 - why the same is 0?
c++ c++11
I have below sample code:
#include <iostream>
#include <vector>
typedef struct defs
{
std::vector<int> myvec;
} dvec;
typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Compile and execute:
$ c++ -std=c++11 try58.cpp
$ ./a.exe
my vec size is 0
I was expecting the size of vector to be 1 - why the same is 0?
c++ c++11
c++ c++11
edited Nov 12 at 14:59
asked Nov 12 at 14:58
Programmer
2,8341649102
2,8341649102
8
What's up with thosetypedef
's everywhere? You don't need them
– Rakete1111
Nov 12 at 14:59
5
Such confusion, much wow
– Zereges
Nov 12 at 15:01
4
@Programmer That's a style used in c becausestruct
doesn't define a type name there. In c++ you don't need totypedef
them sincestruct
create type names.
– François Andrieux
Nov 12 at 15:01
2
dvec1.myvec.push_back(10);
pushes data to a different vector, than you are trying to reference inv1.myvec.size()
.
– Algirdas Preidžius
Nov 12 at 15:02
1
I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05
|
show 2 more comments
8
What's up with thosetypedef
's everywhere? You don't need them
– Rakete1111
Nov 12 at 14:59
5
Such confusion, much wow
– Zereges
Nov 12 at 15:01
4
@Programmer That's a style used in c becausestruct
doesn't define a type name there. In c++ you don't need totypedef
them sincestruct
create type names.
– François Andrieux
Nov 12 at 15:01
2
dvec1.myvec.push_back(10);
pushes data to a different vector, than you are trying to reference inv1.myvec.size()
.
– Algirdas Preidžius
Nov 12 at 15:02
1
I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05
8
8
What's up with those
typedef
's everywhere? You don't need them– Rakete1111
Nov 12 at 14:59
What's up with those
typedef
's everywhere? You don't need them– Rakete1111
Nov 12 at 14:59
5
5
Such confusion, much wow
– Zereges
Nov 12 at 15:01
Such confusion, much wow
– Zereges
Nov 12 at 15:01
4
4
@Programmer That's a style used in c because
struct
doesn't define a type name there. In c++ you don't need to typedef
them since struct
create type names.– François Andrieux
Nov 12 at 15:01
@Programmer That's a style used in c because
struct
doesn't define a type name there. In c++ you don't need to typedef
them since struct
create type names.– François Andrieux
Nov 12 at 15:01
2
2
dvec1.myvec.push_back(10);
pushes data to a different vector, than you are trying to reference in v1.myvec.size()
.– Algirdas Preidžius
Nov 12 at 15:02
dvec1.myvec.push_back(10);
pushes data to a different vector, than you are trying to reference in v1.myvec.size()
.– Algirdas Preidžius
Nov 12 at 15:02
1
1
I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05
I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05
|
show 2 more comments
2 Answers
2
active
oldest
votes
This version, I think, does what you wanted.
#include <iostream>
#include <vector>
struct dvec
{
std::vector<int> myvec;
}
struct myVec : public dvec
{
myVec()
{
myvec.push_back(10);
}
}
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Some notes about what I've changed.
- There's no need to use a
typedef
to create a new type. In C++ bothclass
andstruct
define new types. - When deriving the new type
myVec
fromdvec
, it contains the vectrormyvec
, so there is no need to add a new one. - In the constructor for the derived type
myVec
, we can access themyvec
member of the base type directly, since it is a member of this object.
add a comment |
You have dvecs
as both a parent to myVec
as well as a member of myVec
. In your constructor you push a value to dvec1.myvec
, but in A
you read v1.myvec
aka the member of the parent. You simply have two instances of dvecs
in your structure and you're using different ones in your functions.
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264778%2fvector-size-is-not-correct-how-can-we-initialize-a-vector-inside-a-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This version, I think, does what you wanted.
#include <iostream>
#include <vector>
struct dvec
{
std::vector<int> myvec;
}
struct myVec : public dvec
{
myVec()
{
myvec.push_back(10);
}
}
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Some notes about what I've changed.
- There's no need to use a
typedef
to create a new type. In C++ bothclass
andstruct
define new types. - When deriving the new type
myVec
fromdvec
, it contains the vectrormyvec
, so there is no need to add a new one. - In the constructor for the derived type
myVec
, we can access themyvec
member of the base type directly, since it is a member of this object.
add a comment |
This version, I think, does what you wanted.
#include <iostream>
#include <vector>
struct dvec
{
std::vector<int> myvec;
}
struct myVec : public dvec
{
myVec()
{
myvec.push_back(10);
}
}
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Some notes about what I've changed.
- There's no need to use a
typedef
to create a new type. In C++ bothclass
andstruct
define new types. - When deriving the new type
myVec
fromdvec
, it contains the vectrormyvec
, so there is no need to add a new one. - In the constructor for the derived type
myVec
, we can access themyvec
member of the base type directly, since it is a member of this object.
add a comment |
This version, I think, does what you wanted.
#include <iostream>
#include <vector>
struct dvec
{
std::vector<int> myvec;
}
struct myVec : public dvec
{
myVec()
{
myvec.push_back(10);
}
}
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Some notes about what I've changed.
- There's no need to use a
typedef
to create a new type. In C++ bothclass
andstruct
define new types. - When deriving the new type
myVec
fromdvec
, it contains the vectrormyvec
, so there is no need to add a new one. - In the constructor for the derived type
myVec
, we can access themyvec
member of the base type directly, since it is a member of this object.
This version, I think, does what you wanted.
#include <iostream>
#include <vector>
struct dvec
{
std::vector<int> myvec;
}
struct myVec : public dvec
{
myVec()
{
myvec.push_back(10);
}
}
class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};
int main()
{
A a1;
}
Some notes about what I've changed.
- There's no need to use a
typedef
to create a new type. In C++ bothclass
andstruct
define new types. - When deriving the new type
myVec
fromdvec
, it contains the vectrormyvec
, so there is no need to add a new one. - In the constructor for the derived type
myVec
, we can access themyvec
member of the base type directly, since it is a member of this object.
answered Nov 12 at 15:19
Tim Randall
1,9221119
1,9221119
add a comment |
add a comment |
You have dvecs
as both a parent to myVec
as well as a member of myVec
. In your constructor you push a value to dvec1.myvec
, but in A
you read v1.myvec
aka the member of the parent. You simply have two instances of dvecs
in your structure and you're using different ones in your functions.
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
add a comment |
You have dvecs
as both a parent to myVec
as well as a member of myVec
. In your constructor you push a value to dvec1.myvec
, but in A
you read v1.myvec
aka the member of the parent. You simply have two instances of dvecs
in your structure and you're using different ones in your functions.
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
add a comment |
You have dvecs
as both a parent to myVec
as well as a member of myVec
. In your constructor you push a value to dvec1.myvec
, but in A
you read v1.myvec
aka the member of the parent. You simply have two instances of dvecs
in your structure and you're using different ones in your functions.
You have dvecs
as both a parent to myVec
as well as a member of myVec
. In your constructor you push a value to dvec1.myvec
, but in A
you read v1.myvec
aka the member of the parent. You simply have two instances of dvecs
in your structure and you're using different ones in your functions.
answered Nov 12 at 15:03
Kiskae
12.3k12538
12.3k12538
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
add a comment |
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
– Programmer
Nov 12 at 15:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264778%2fvector-size-is-not-correct-how-can-we-initialize-a-vector-inside-a-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
What's up with those
typedef
's everywhere? You don't need them– Rakete1111
Nov 12 at 14:59
5
Such confusion, much wow
– Zereges
Nov 12 at 15:01
4
@Programmer That's a style used in c because
struct
doesn't define a type name there. In c++ you don't need totypedef
them sincestruct
create type names.– François Andrieux
Nov 12 at 15:01
2
dvec1.myvec.push_back(10);
pushes data to a different vector, than you are trying to reference inv1.myvec.size()
.– Algirdas Preidžius
Nov 12 at 15:02
1
I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05