Using a struct variable as a formal parameter - c++

#include <iostream>
using namespace std;
void input(partsType inventory[100]&)
{}
int main()
{
struct partsType
{
string partName;
int partNum;
double price;
int quantitiesInStock;
};
partsType inventory[100];
}
I am trying to use a struct variable as a formal parameter. Later I will pass the variable by reference.
Currently, I am getting the error
declaration is incompatible, and `partsType` is undefined.

You have two issues:
You need to define the partsType outside the main, and of-course
before the input function otherwise, it does not know what is partsType.
Secondly, your function parameter syntax is wrong. It should have
been
void input(partsType (&inventory)[100])
// ^^^^^^^^^^^^^^^^^^ --> if you meant to pass the array by ref
So you need:
#include <iostream>
#include <string> // missing header
struct partsType
{
std::string partName;
int partNum;
double price;
int quantitiesInStock;
};
void input(partsType (&inventory)[100])
{}
int main()
{
partsType inventory[100];
}
Another option is to forward declare the struct partsType before the input function. But, that will need to make the function definition after the main, as you define the struct inside the main:
#include <iostream>
#include <string> // missing header
// forward declaration
struct partsType;
void input(partsType(&inventory)[100]);
int main()
{
struct partsType
{
std::string partName;
int partNum;
double price;
int quantitiesInStock;
};
partsType inventory[100];
}
void input(partsType(&inventory)[100])
{
// define
}
Also do not practice with using namespace std;

Related

How to create a function inside a structure in a header file and implement this function in another c source file?

Here I created a main.c
#include <iostream>
#include "Header.h"
struct person_t a;
int main()
{
a.func(1,2);
}
And I have a header called "Header.h", I define a structure in this file
struct person_t {
char name;
unsigned age;
int(*func)(int, int);
};
Then I implement int(*func)(int, int); in another c file called "Source.c"
#include "Header1.h"
struct person_t a;
int add2(int x, int y)
{
return x + y;
};
a.func = &add2;
But it seems not work, does anyone who has any idea of this question?
You may not use statements outside a function like this
a.func = &add2;
Remove this statement and this declaration
struct person_t a;
from Source.c.
In the header place the function declaration
int add2(int x, int y);
And in the file with main write
int main()
{
struct person_t a;
a.func = add2;
a.func(1,2);
}

"declaration is incompatible with "void Student::update()" " Showing this error at 41st line. how to correct it?

code
#include <iostream>
#include <string>
#include <algorithm>
using namespace std ;
class Student{
public:
int id;
string name;
int roll;
static string school;
static int class;
void input();
void output();
static void update();
};
string Student::school = "PPS";
int Student::class = 7;
void Student::update(int x){
class=8;
}
int main(){
Student s;
return 0;
}
issue
"declaration is incompatible with "void Student::update()" (declared at line 16) " showing this error.
I want to declare a variable in the the function which is present in the class.
Compare
static void update()
to
void Student::update(int x)
Note how one has an int parameter and one doesn't.
Since you don't access it in your implementation of update, I suppose it should be removed in the definition.

How can I extern namespaces in a namespace?

In my previous question, I asked that how can I extern classes in a class, and we could do:
namespace kc
{
using ::A;
using ::B;
}
instead of:
namespace kc
{
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
}
And now, I want to do something like this for namespaces, without defining the entire namespace again. I tried to use the same code, but it doesn't work: error: using-declaration may not name namespace <namespace_name>
Edit
Actually, I'm making a kernel, and that's my code (pio.hpp):
#pragma once
namespace pio
{
void outb(unsigned short port, unsigned char value);
void outw(unsigned short port, unsigned short value);
unsigned char inb(unsigned short port);
unsigned short inw(unsigned short port);
}
And (k.hpp):
#pragma once
#include <pio.hpp>
#include <cursor.hpp>
namespace k
{
using ::pio;
using ::cursor;
}
I think what you’re looking for is a namespace alias:
namespace A {
namespace B {
struct C {};
}
}
namespace X {
namespace Y = ::A::B;
}
This would allow you to write e.g.
X::Y::C foo;

Two errors using class and header

I get the error class redefinition
Also the error ID is not a member of class process
and missing semi colon before ID
I tried char* instead of string.. not working also
Any help? I seem to be missing something
Thanks in advance
process.h file
#ifndef PROCESS_H
#define PROCESS_H
class process
{
public:
string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
process.cpp file
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
class process
{
public:
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
};
Problem 1 Class refinition
In cpp file you don't need to write class process. It should be
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
In header file you have given required declarations. In cpp, you need to give the definitions. But because you are outside the class scope now, you must give the fully qualified name of members which you are already doing. (for ex: int process:: get_finish_time(int start, int run))
If you again use class process, compiler has no hint that you intend the same class and not want a new class causing the class redifinition error. In C++, it is not allowed to make a class partially and complete the rest of it somewhere else. (Not to be cofused with inheritance)
Problem 2: string issue
Add #include <string> in your header file and use std::string or add line using std::string
There are a few issues. One is this:
process.h needs to #include <string>. Once included, prepend all string variables with std::.
#ifndef PROCESS_H
#define PROCESS_H
#include <string>
class process
{
public:
std::string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
The problem with your original code is that you're:
Relying that some outside module included <string> before process.h is included.
Relying that some outside module stated using namespace std; before including process.h.
None of those can be guaranteed.

no instance of overloaded function matches the argument list c++

I'm a little confused by this error that I'm having (I'm using VS2012).
Here's my code:
RecipeBook.h:
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;
class RecipeBook
{
private:
vector<SingleRecipe> *recipe;
SingleRecipe *one;
public:
RecipeBook(vector<SingleRecipe> *recipe);
void addRecipe(SingleRecipe *one);
bool removeRecipe(string name);
vector <SingleRecipe> *returnListOfRecipes(double time);
};
#endif
SingleRecipe.h:
#ifndef SINGLERECIPE_H
#define SINGLERECIPE_H
#include <string>
#include <vector>
using namespace std;
class SingleRecipe
{
private:
string name;
vector<string> ingredients;
vector<string> method;
int numOfServing;
double time;
public:
SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time);
string getName();
void setName();
int getNumOfServing();
void setNumOfServing();
double getTime();
void setTime();
string toString();
};
#endif
BookAndRecipe.cpp:
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;
vector <SingleRecipe> *RecipeBook::returnListOfRecipes(double time)
{
vector<SingleRecipe> *two;
for (int i = 0; i = recipe->size(); i++)
{
if (recipe[i].data()->getTime < time)
{
*two->push_back(recipe[i].pop_back());
}
}
return NULL;
}
Over at returnListOfRecipes() I get this error:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=SingleRecipe, _Alloc=std::allocator<SingleRecipe>]" matches the argument list
argument types are: (void)
object type is: std::vector<SingleRecipe, std::allocator<SingleRecipe>> c:\Users\Ventus\Documents\Visual Studio 2012\Projects\Recipe\Recipe\BookAndRecipe.cpp 83 8
I suspect it might have something wrong with my for loop, but I'm not very experienced, so I might be doing something very wrong here.
I appreciate all help that's given!
pop_back() doesn't return a value, it just drops the last element of the container. You probably want:
*two->push_back(recipe[i].back());
recipe[i].pop_back();