Declaring a operator+ function in C++ [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
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

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;
}

Passing a vector object to a function 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
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).

Using parallel_sort algorithm on a struct [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
How to use parallel_sort with this struct:
struct d
{
char name[5];
};
struct less_than_key
{
inline bool operator() (const d& struct1, const d& struct2) const
{
return strcmp(struct1.name, struct2.name) == -1;
}
};
vector<d> my_d;
my_d.resize(3);
strcpy(my_d[0].name, "mike");
strcpy(my_d[1].name, "joe");
strcpy(my_d[2].name, "anna");
parallel_sort(my_d.begin(), my_d.end(), less_than_key());
DONE!, I hope this helps someone else.
Guess it's time to take my Phd in sorting algorithms!
Don't use C-style strings when you can easily use std::string.
Since your struct only contains a string you may want to just use std::string instead (which implements the operators that you need for parallel_sort to work).
std::vector<std::string> vec(3);
vec.emplace_back("mike");
vec.emplace_back("joe");
vec.emplace_back("anna");
and then, following the function signature, you just need to find the begin and end iterator:
parallel_sort(std::begin(vec), std::end(vec));

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;
}

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.