-6
        // pointer to functions
#include <iostream>
#include <vector>
using namespace std;

struct boo{
    string u;
    int p;
};

vector <boo> om(boo &bo){
    cin >> bo.u;
    cout << bo.u;
}

int main(){
    string q;
    vector <boo> b;
    om(b);
}

Ok I need to understand how will I be able to write vector<boo> om(boo &bo) into my main(). I always get some type of error with it and I need help with it. I can't ever call the function into main because I just simply don't know how to call it into main. Well I know how to call om([I just need help with this part]) I have no idea what to fill this in with.

P.S Sorry this is at the bottom Im a noob with stackoverflow.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
Justin
  • 1
  • 1
  • 4
  • 3
    b should be of type boo, not vector – Creris Jul 18 '15 at 23:19
  • 2
    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – juanchopanza Jul 18 '15 at 23:21
  • 1
    learn the basics mate, you are confusing alot of basic concepts –  Jul 18 '15 at 23:22
  • But it also keeps crashing and believe it or not but I have been coding for about 9 months now. But it keeps crashing I don't want that so i assumed I am doing something wrong. – Justin Jul 18 '15 at 23:28
  • Are you not missing a semicolon between `vector { ... }` and `int main` ? (not sure, it's ten years since I last had a c++ job) – Dirk Horsten Jul 18 '15 at 23:39
  • 3
    Error messages contain all the necessary information. 1) You don't return value from function `om`. 2) You try to pass incorrect type to `om`. However, I can't tell you how to repair this, because I have no idea what this code is supposed to do. – Piotr Siupa Jul 18 '15 at 23:43
  • 1
    @DirkHorsten `C++` does not require semicolon after function definition. – Piotr Siupa Jul 18 '15 at 23:45
  • @NO_NAME Thanks for refreshing my memory – Dirk Horsten Jul 18 '15 at 23:47
  • Ok I figured it out somewhat it still crash for some damn reason even if I return into the parameters It still crash – Justin Jul 18 '15 at 23:51
  • 2
    Justin, ask the real question: "I have this code. It does this. I want it to do that." – user4581301 Jul 18 '15 at 23:53
  • I'm glad you've used clear and non-confusing variable names, Justin. – Lightness Races in Orbit Jul 19 '15 at 00:13
  • @NO_NAME: I'm curious; why did you write "C++" in source code formatting style just then? – Lightness Races in Orbit Jul 19 '15 at 00:14
  • 2
    @Justin A good question can be understood (and answered) without adding any info, and this is not the case, as we don't understand what you want to do. This wouldn't even be too bad, _if you took the time to read the comments and answer by explaining what you want_. Do you realize some people are dedicating some of their time to you, and you are not even bothering to dedicate some of your time to them (us)? Please read NO_NAME's and user4581301's comments (at least) and answer. – Fabio says Reinstate Monica Jul 19 '15 at 00:19
  • @LightnessRacesinOrbit Good point. I saw that someone else is doing it and I never think about whether it's a good idea. – Piotr Siupa Jul 19 '15 at 00:26
  • @user4581301: That is not a question. – Lightness Races in Orbit Jul 19 '15 at 00:30
  • I want it call the function om in main without it crashing. Passing something in om without it crashing. – Justin Jul 19 '15 at 02:51
  • @FabioTurati I have read their comments and I have took it into consideration. I have tried putting it into my code to try to solve my problem ,but it didn't work. So I found it out it just always crash (see previous comment) – Justin Jul 19 '15 at 02:59
  • @FabioTurati gave me the best answer of them all really. Honestly – Justin Jul 19 '15 at 03:01
  • @LightnessRacesinOrbit I have to grant you that. It is more a demand. Justin, what is function om supposed to do? Until the purpose of om is known, no one can tell you how to write and call it. My best guess is read in input to get the data required to fill out a boo structure and then place that boo structure into vector b. – user4581301 Jul 19 '15 at 04:24

1 Answers1

3

Taking a shot in the dark here because this seems to be the most likely intent. If i'm wrong, hopefully it is still instructional.

#include <iostream>
#include <vector>

I removed the using namespace std that was here because it's dangerous. Don't believe me? Do a quick search. StackOverflow is littered with "WTF is going on?" questions that resolve down to "something in the std namespace had the same name as your variable."

struct boo 
{
    std::string u; 
    int p;          
};

I've left Boo alone to keep it recognizable to the OP. However, descriptive names really help your code explain itself. No one here, excluding the OP, has any clue what a boo represents and how it should be used or what it's members represent and how they should be used. This limits the our ability to assist the OP with debugging support and advice.

On to function om. Again, terrible name. A function name should provide some hints as to what the function does.

Assumptions:

Om is intended to

  1. read in input to get the data required to fill out a boo structure
  2. place that boo structure into main's vector b

With that in mind,

  1. No need to return anything except perhaps an error code if the user fails to enter correct input.
  2. The only thing worth passing in is a reference to the vector. The reference allows the vector supplied by the caller to be modified.

If we knew what a boo was someone might be able to suggest better ways to do this and validate the user input.

void om(std::vector<boo> &bo)
{
    boo temp; //start by making a temporary boo
    std::cin >> temp.u; // read user input into the temporary boo
    // removed the print out here
    bo.push_back(temp); // copy the temporary boo into the vector
}

main is altered to remove unused string q and output the contents of vector b

int main()
{
    std::vector<boo> b; // usual nagging about non-descriptive name
    om(b);
    for (boo testboo: b)
    { // print all boos in b
        std::cout << testboo.u << std::endl;
    }
}

And as one cut-n-paste-able block for trying it out and playing around:

#include <iostream>
#include <vector>

struct boo
{
    std::string u;
    int p;
};

void om(std::vector<boo> &bo)
{
    boo temp; //start by making a temporary boo
    std::cin >> temp.u; // read user input into the temporary boo
    // removed the print out here
    bo.push_back(temp); // copy the temporary boo into the vector
}

int main()
{
    std::vector<boo> b; // usual nagging about non-descriptive name
    om(b);
    for (boo testboo: b)
    { // print all boos in b
        std::cout << testboo.u << std::endl;
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54