I am having this problem for a few days now and I can not find a way around it.
I'm trying to store an array of cv::Mats and the way I was doing this so far was by having a a
std::vector<cv::Mat> store_frames(32); and every time I was just copying the new frames to their location with frame.copyTo(store_frames[i]).
This works fine if it's inside main but if I want to pass it to a function and do the same thing it's not good anymore.
I did not have the same problem with simple mats. I was just declaring them globally inside a header
cv::Mat frame;
And if I had a function:
void function( cv::Mat &f1 ) {
..
f1.at<float>(10,10) += 100;
..
}
Then call the function function(frame);
All the changes inside the function would be saved. So let's say I will have something like
...
for(int i=0; i<5; i++) {
function(frame);
std::cout<<frame.at<float>(10,10)<<' ';
}
The output (assuming the initial value of frame.at(10,10) would be
100 200 300 400 500
The problem now is that I can not do the same thing with std::vector store_frames . First of all if I try to declare it in the header the same way std::vector<cv::Mat> store_frames(32); I get an error on number 32 saying Error: expected a type specifier. And if I just say std::vector<cv::Mat> store_frames; then it won't know it's size. I saw somewhere people calling store_frames.resize I've tried to do that, but I get an error saying this declaration has no storage class or type specifier. I've looked up the internet for solutions, but didn't find any. I know the question itself might sound stupid, but I'm new to this and I don't know much.
So my questions is how do I declare this vector so that it would be possible to pass it to a function, modify it inside and the modifications would be saved?
Thank you!
Try
declared it std::vector<cv::Mat> store_frame; in the header and store_frame.resize(32); in the source code.
use std::vector<cv::Mat> store_frames(32); in the source code.
ps: I just added this as the comment. :)
Related
So I am really unsure about how to even ask this question, so it was hard for me to look this up. I made the title the best that I could to fit the question. So, lets get into it.
I am making a project to run the game fore&aft, and I need to be able to pass an array into a function. So right now I have something similar to this:
//global
const int sizeFive = 5;
//functions
void function(string m[][sizeFive]){}
int main(){
string GAME_BOARD[sizeFive][sizeFive];
//populate GAME_BOARD here
function(GAME_BOARD);
}
Now this works for the game board on a 5x5 board. However, one of the requirements for the project is that it also needs to work on a 7x7, 9x9, and 11x11 board size.
I also have the following global variables.
int SIZE; //size that is not const and assigned in main from user input
const int sizeSeven = 7; //size I want to use for 7x7 board
const int sizeNine = 9; //size I want to use for 9x9 board
const int sizeEleven = 11; //size I want to use for 11x11 board
So is it possible to make my function calls change, maybe by using SIZE, after the user inputs the size of the board they want? If so, how is this possible? Right now the best idea I had was to make 4 different files, 1 for each size, and let it call those after the user inputs the size in the driver file. However, this seems ineffecient and seems like it will take up more space than really needed (not optimal). Thanks in advance for any help you are able to give!
The approach you have is flawed and will lead nowhere. The simple reason is that the sizes of an array are part of its type, which are also part of the function signature. This is set at compile time and constant from there on. No user-supplied input can ever change this.
Now, how can you fix this? The first thing to do is to create an object that holds the field. This object provides access to the fields (like the array) and also provides the size (to get rid of the nasty global). The difference is that it can be initialized to different sizes.
Your function() doesn't loop from 0 to sizeFive then but from 0 to field.size(), just to give an example. Instead of accessing the raw array as field[x][y] it requests the fields like field.get(x, y).
I am currently creating a array that holds a bunch of different data. One of the data points is to hold the most recent date accessed. For some reason I can't put year() into the array without the program breaking. Code below.
world_saves.push([[year()]]);
What's the reasoning behind this, and how do I set put the year into the array without a error. I've tried defining year() into a variable, but once again I get a error. Error below.
World_Data.js:5 Uncaught ReferenceError: year is not defined
Thanks in advance!
EDIT:
For anyone viewing this, this is the correct code to use.
var pjs = new Processing();
world_saves.push([[pjs.year()]]);
With the caveat that I've never used processing.js, it appears that you have to instantiate an instance of it first before calling methods on it.
Something like:
var pjs = new Processing();
world_saves.push([[pjs.year()]]);
also note: You do realize you're pushing an array of an array containing the year here, right? (vs. just world_saves.push(pjs.year());)
I see that you already got this sorted out, but just for your information: with Processing.js, typically you'd write Processing code and have Processing.js create the Processing instance and call the setup() and draw() functions for you. There are a ton of ways to do this (many are outline on this page), but the basics would look like this:
<script src="processing-1.3.6.min.js"></script>
<script type="application/processing" data-processing-target="pjs">
int[] worldSaves = new int[1];
void setup() {
size(200, 200);
worldSaves[0] = year();
}
void draw(){
background(0);
text(worldSaves[0], 10, 10);
}
</script>
<canvas id="pjs"> </canvas>
What you came up with isn't really wrong or anything, but I also don't know why you're using Processing.js if you aren't using the Processing part of it. So eventually you might end up with something more like the above.
So when writing a game on Khan Academy When I try to remove a bullet from the array I run into the error "Object does not support method splice" I have been checking my code for hours and have not found out why it does not work. Ideas?
EDIT: The code used to remove a bullet is bullets[i].splice(i,1); and that is what errors out my code.
MVCE:
var bullets = [];
var bullet= function(x,y,blah)
{
//code that is not important here
};
bullets.push(bullet(0,0,30));
for(var I = 0; I < bullets.length; I++){
if(bulletRemove){
bullets[I].splice(i,1)
}
}
You have a variable named bullets:
var bullets = [];
(Side note: Why is there a random curly bracket right before this line?)
This bullets variable is an array. It holds instances of the Bullet class:
bullets.push(new Bullet(x, y, 10, player.x+bSize/2, player.y+bSize/2));
You can use the array to access a Bullet at a particular index, and then you can call functions of the Bullet class on that instance:
bullets[i].move();
You can also call the splice() function on the array itself:
bullets.splice(i,1);
However, you can't call the splice() function on a particular Bullet instance!
bullets[i].splice(i,1);
This line is taking an instance of Bullet from the i index of the bullets array, and then trying to call the splice() function from the Bullet class. But the Bullet class doesn't have a splice() function! This is what's causing the error.
Instead, you probably meant to call it on the array itself:
bullets.splice(i,1);
In the future, please please please try to narrow your problem down before posting a question. Try to post an MCVE instead of your entire project. You could have put together an example program that used just a few lines to create a hard-coded array and used that to demonstrate your problem. Chances are you would have found the problem yourself in the process of creating the MCVE!
I started learning C in class and have started the assignment but I'm writing it one piece at a time. Currently I'm stuck because I may be doing something completely wrong but I don't know what I'm doing.
The error is pretty clear... The void type isn't something you can return / store in a variable. Instead it's a special type that means "nothing". You can't store nothing, you must ignore the return or return something.
displayCard only displays the cards, it does nothing to define them, so you shouldn't be trying to use it to create your cards. Instead, think about what a "card" actually is in your program--what, for example, do you have to pass to displayCard in order for it to know which card to display?
I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC
Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.
When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?