Passing a vector object to a function by reference? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I correctly ask for object rock in main. I simplified the code, figuring this was the only problem. The error "expected primary-expression before '&' token.
void createObject(vector <object>& obj, world wld)
{
....
}
int main()
{
object rock;
createObject(vector<object>& rock, level_1);
return 0;
}

Very simply:
int main()
{
std::vector<object> rock_vector(1);
createObject(rock_vector, level_1);
}
You can't pass rock to it, as it's not a vector. You need to pass an actual vector to it. Here, I made rock_vector of size 1, so it's at least got one object in it (so rock_vector[0] is more or less your replacement for rock).

Related

Pointer to function with defined type [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have function declaration as int StartSale(CTXNSession & txnsession) where CTXNSession is a class.
I need to create pointer to this function.
I tried to achieve it like this:
int (*pFct)(CTXNSession&);
But I got compilation error.
But if the argument was of a predefined type , there was no error.
Please assist
Your declaration of function pointer is correct. Perhaps you forgot to declare the class? Or maybe some other error.
This code compiles without errors:
class CTXNSession; // your class
int StartSale(CTXNSession & txnsession) {} // your function
int main() {
int (*pFct)(CTXNSession&); // function pointer declaration
pFct = StartSale; // assignment for a bonus
return 0;
}

can a c vector store more than one data type? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to write a function that will return 4 members of a class. They are 3 ints and char, and I'd like to store them all in one vector and return it from a function call. Can I do that?
You either need an std::tuple if you want to preserve the types and if the result length is constant, or just cast all members to some common supertype and store them in a container.
You need a class:
struct S
{
int a, b, c;
char letter;
};
int main()
{
S s;
}

c++: Coping to an stl container that is passed by reference [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using the following code:
class MyClass
{
public:
void GetStrings(list<std::string>& strs)
{
strs = m_strings;
}
list<std::string> m_strings;
}
Are all m_strings's elements being copied when strs = m_strings; is performed?
In other words:
Is this equal to list<std::string>& strs = m_strings; or to list<std::string> strs = m_strings; (with respect to elements copy)
yes.
Now, if you wanted to copy just a reference to the list (like you have in languages such as C# or Java) then pass a shared_ptr instead, then you will have a single list with 2 references to it.
All variable assignment in C/C++ is done by value, even if the variable being passed is a pointer (eg a 4 byte variable). Other languages are the same, its just that they cover this up with their language constructs.

Declaring a operator+ function in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a class Samp. In Samp.cpp, I can define/declare a function like
Samp& operator+(Samp& other) {
std::cout << "something";
return other;
}
What is this function exactly? How do I call it?
This is actually a unary +, you call it like this:
Samp s;
+s; // <-- here

Convert string array to char ** [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
string str[3];
void foo(char** str)
{
//do something to str...
}
How to pass str[] to function foo in a convenient way?
The function expects an array of pointers, so you'll have to make one from your array of strings:
std::vector<char*> pointers;
for (auto & s : str) {
pointers.push_back(&s[0]);
}
foo(&pointers[0]);
Beware that this may not be valid if the function modifies the pointers, or the strings they point to. A better option would be to avoid mixing C and C++ style string handling, if possible.