Why can't I access the size of this C++ array?
I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];
while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}
for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}
This code is throwing, among other things, an error saying:
"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"
Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?
Thanks in advance!
c++ algorithm data-structures deque
add a comment |
I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];
while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}
for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}
This code is throwing, among other things, an error saying:
"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"
Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?
Thanks in advance!
c++ algorithm data-structures deque
2
You need astd::vectororstd::arrayto do that. Raw arrays don not have any class member functions.
– πάντα ῥεῖ
Nov 27 '18 at 7:18
1
Possible duplicate of How do I find the length of an array?
– abcalphabet
Nov 27 '18 at 7:23
add a comment |
I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];
while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}
for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}
This code is throwing, among other things, an error saying:
"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"
Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?
Thanks in advance!
c++ algorithm data-structures deque
I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];
while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}
for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}
This code is throwing, among other things, an error saying:
"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"
Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?
Thanks in advance!
c++ algorithm data-structures deque
c++ algorithm data-structures deque
edited Nov 28 '18 at 0:01
jww
53.7k40231508
53.7k40231508
asked Nov 27 '18 at 7:16
adam troppadam tropp
8818
8818
2
You need astd::vectororstd::arrayto do that. Raw arrays don not have any class member functions.
– πάντα ῥεῖ
Nov 27 '18 at 7:18
1
Possible duplicate of How do I find the length of an array?
– abcalphabet
Nov 27 '18 at 7:23
add a comment |
2
You need astd::vectororstd::arrayto do that. Raw arrays don not have any class member functions.
– πάντα ῥεῖ
Nov 27 '18 at 7:18
1
Possible duplicate of How do I find the length of an array?
– abcalphabet
Nov 27 '18 at 7:23
2
2
You need a
std::vector or std::array to do that. Raw arrays don not have any class member functions.– πάντα ῥεῖ
Nov 27 '18 at 7:18
You need a
std::vector or std::array to do that. Raw arrays don not have any class member functions.– πάντα ῥεῖ
Nov 27 '18 at 7:18
1
1
Possible duplicate of How do I find the length of an array?
– abcalphabet
Nov 27 '18 at 7:23
Possible duplicate of How do I find the length of an array?
– abcalphabet
Nov 27 '18 at 7:23
add a comment |
2 Answers
2
active
oldest
votes
A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here
void printKMax(int arr, int n, int k){
Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.
add a comment |
you can use this statement to get length of array
int length = (sizeof(arr)/sizeof(*arr));
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
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%2f53494526%2fwhy-cant-i-access-the-size-of-this-c-array%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
A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here
void printKMax(int arr, int n, int k){
Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.
add a comment |
A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here
void printKMax(int arr, int n, int k){
Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.
add a comment |
A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here
void printKMax(int arr, int n, int k){
Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.
A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here
void printKMax(int arr, int n, int k){
Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.
edited Nov 27 '18 at 7:26
answered Nov 27 '18 at 7:21
BlazeBlaze
6,8081732
6,8081732
add a comment |
add a comment |
you can use this statement to get length of array
int length = (sizeof(arr)/sizeof(*arr));
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
add a comment |
you can use this statement to get length of array
int length = (sizeof(arr)/sizeof(*arr));
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
add a comment |
you can use this statement to get length of array
int length = (sizeof(arr)/sizeof(*arr));
you can use this statement to get length of array
int length = (sizeof(arr)/sizeof(*arr));
answered Nov 27 '18 at 7:25
JinJin
9311323
9311323
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
add a comment |
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
Will not work in this case.
– DeiDei
Nov 27 '18 at 9:09
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.
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%2f53494526%2fwhy-cant-i-access-the-size-of-this-c-array%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
2
You need a
std::vectororstd::arrayto do that. Raw arrays don not have any class member functions.– πάντα ῥεῖ
Nov 27 '18 at 7:18
1
Possible duplicate of How do I find the length of an array?
– abcalphabet
Nov 27 '18 at 7:23