In a data-structure, how do you insert a function?
struct Student_info {
std::string name;
double midterm, final;
unsigned int& counter;
std::vector<double> homework;
double overall = grade(students[counter]);
};
always get this type of error:-
a. "variable" was not declared in this code.
b. "Student_info::counter" cannot appear in a constant-expression.
c. an array reference cannot appear in a constant-expression.
d. a function call cannot appear in a constant-expression
edit:-
oopps, i mean student_info contain in a vector, wait, why that's info needed anyway... Dx
oh, and btw, this is from Accelerated C++, a book obviously, and I'm trying to answer one of its exercise, then I need to know this part, not found any on the book Dx
the question is 4-6. Rewrite the Student_info structure to calculate the grades immediately and store only the final grade.
You can NOT dynamically insert a function into a structure.
You can declare a structure that has a method()
struct Student_info
{
void doDomethingToStudent()
{
// Manipulate the object here.
}
// STUFF
};
Also you can not initialize member like above.
double overall = grade(students[counter]);
Here you need to create constructor that will initialize members.
struct Student_info
{
Student_info(std::string& studentName, unsigned int& externalCounter)
: name(studentName)
, midterm(0)
, final(0)
, counter(externalCounter)
, homework()
// It is not clear if overall is a normal memeber
// Or a static member of the class
, overall(grade(students[counter]))
{}
// STUFF
};
int main()
{
unsigned int counter = 0;
Student_info bob("Bob", counter);
}
Related
Recently, I have seen this code:
class Student; // forward declaration
class Teacher
{
friend void registration(Teacher &t, Student &s);
public:
void setGrades(); // sets students' grades
protected:
int numStudents;
Student *ptrList[100]; // <--- ???
};
That looks like a mixture of pointer and array...
Usually, it is either int *ptr or int array[10]
I have never seen something like this. Can someone explain this to me?
You have an array of pointers to type Student. Think about it this way:
A typical declaration for an array in C++ is:
name [elements];
In the provided example the <type> used for the elements of the array is Student*, which is a pointer to type Student.
I have a take home test that it work a large percentage of my grade. These 3 questions are on it and they are worth half the test. I was wondering if someone could kindly review my quiz and make sure they are correct for me. I’m a worrier and I want to make sure they are correct. Thank you in advance!
Implement the constructor for the class called "SimpleMath". The constructor takes two integer parameters; "var1" and "var2". The constructor is to store the value that was passed into "var1" into the private integer member variable "m_value1" and the value that was passed into "var2" into the private integer member variable "m_value2"
class SimpleMath
{
public:
SimpleMath(int var1, int var2) : m_value1(var1), m_value2(var2) {};
int getVar1() const
{
return m_value1;
}
int getVar2() const
{
return m_value2;
}
private:
int m_value1;
int m_value2;
};
Implement the "Multiply" method for the "SimpleMath" class. This method does not require any parameters and returns an integer value. This method should multiply the values stored in the classes private integer member variables "m_value1" and "m_value2" the resulting value is returned. Assume that "m_value1" and "m_value2" were loaded inside the classes constructor.
class SimpleMath
{
public:
SimpleMath(int var1, int var2);
int Mutiply= m_value1* m_value2;
private:
int m_value1;
int m_value2;
};
Write a class definition called "SimpleMath" that has a constructor that takes two integers "var1" and "var2". It has four public methods that take no parameters and return an integer value; "Add","Subract","Divide" and "Multiply". The class has two private member variables of type integer; "m_value1" and "m_value2".
class SimpleMath
{
SimpleMath(int var1, int var2);
public:
int Add;
int Subract;
int Divide;
int Multiply;
private:
int m_value1;
int m_value2;
};
Am I to assume you have no prior programming experience?
At any rate, the first part looks ok. What the : denotes is the initializer list. It can, and is, correctly used to initialize a class's members.
Second part, you got it dead wrong I am afraid.
First of, you've not implemented the method Multiply() there, you've just declared a variable.
Instead do this:
int Multiply()
{
int sum = m_value1 * m_value2;
return sum;
}
For brevity's sake, you can also do this:
int Multiply()
{
return m_value1 * m_value2;
}
Does the same thing.
Also note that the answer from question 1 has not carried over to question 2, ie the constructor is incomplete. It won't compile.
As for question 3, it's just question 2 all over again, except you've also got to implemented the three remaining arithmetic functions. Suffice it to say, I am sure you can figure it out.
I'll try to make this as concise as possible and while I understand that these questions can be considered "basic" I have already looked at websites such as cplusplus.com and yolinux tutorials but i need somebody to explain this to me like I have just had a major head trauma..
1)
class Rectangle {
private:
int lineNumber; // LineNumber of the ACSIL Tool
float valueMax; // value of the higher limit of the rectangle
float valueMin; // value of the lower limit of the rectangle
public:
Rectangle(SCStudyInterfaceRef sc, int lineNumber, float valueMax, float valueMin);
int getLineNumber();
float getValueMax();
float getValueMin();
};
So int linenumber, valueMax and ValueMin are declared private members and thus are only accessible by members of the same class, thats fine. But what about the part that follows the "public:" ?
a) Is Rectangle(SCStudyInterfaceRef sc, int lineNumber, float valueMax, float valueMin); a function that is being overloaded? and if yes are int getLineNumber() etc part of that function or seperate members of the public part of the class?
2)
Rectangle::Rectangle(SCStudyInterfaceRef sc, int lineNumber0, float value1, float value2) {
lineNumber = lineNumber0;
int value2_greater_than_value1 = sc.FormattedEvaluate(value2, sc.BaseGraphValueFormat, GREATER_OPERATOR, value1, sc.BaseGraphValueFormat);
if (value2_greater_than_value1 == 1) {
valueMax = value2;
valueMin = value1;
} else {
valueMax = value1;
valueMin = value2;
}
}
int Rectangle::getLineNumber() {
return lineNumber;
}
float Rectangle::getValueMax() {
return valueMax;
}
float Rectangle::getValueMin() {
return valueMin;
}
a) I'm pretty sure that the functions defined inside the public part of the rectangle class are being "defined" here, or something along those lines.
b) I am really confused about what is happening here on the Rectangle::Rectangle(SCStudyInterfaceRef sc, int linenumber0, float value1, float value2) part. I understand the logic of what is happening within the function itself but i am confused about the paramters being input within the " ( ) " and how exactly this relates to what happenes inside the class public part. This really is the most important question that needs answering.
I have tried to be as concise and onpoint as possible, would appreciate some help in understanding this syntax.
Question 1
It's a constructor with 4 parameters.
int getLineNumber();
float getValueMax();
float getValueMin();
are all member functions in the class.
Question 2
The constructor defined earlier is called with 4 parameters. If no other constructor is defined then you'll have to instantiate the class with exactly 4 parameters, i.e:
Rectangle *rect = new Rectangle(sc, 100, 1.2, 6.8);
or simply:
Rectangle rect(sc, 100, 1.2, 6.8);
These parameteres are then used to "set the object in an initial state".
The member functions are used to get various values in their current (or final or only) state.
Rectangle::Rectangle is the class constructor. It is called whenever a Rectangle object is created. Read about constructors to understand better.
The constructor is setting initial values for the valueMax and valueMin member variables. It uses the parameters passed to the constructor to do this. Read about function parameters to understand better.
1) a: If no ctor function is declared, then the compiler writes a ctor for the class. But when a ctor is provided by the class no default ctor is written by the class and hence no overloading is taking place. Now if you go on and define one more ctor, may be because you want the object to be constructed in some other way, then you will have an overloaded ctor. In your case no overloading is taking place.
int getLineNumber() is just another member of the class.
2)
a: You are correct.
b: The parameters put inside "( )" are arguments list and if this function is called somewhere, then this list is type-matched and then function is called(in case of overloading). Now if you write a statement like:
Rectangle x(a, b, c, d);
then it means that your sc=a, lineNumber0=b, value1=c, value2=d for this function call.
I am working on an c++ hw assignment so I will try not to post too much code as possible, what we are working on is as following: we have a class that include a public swap function (along with insert and delete functions and such) and a private struct array to store the data.
something like:
Class set
public:
set(int dimension);
insert();
delete();
swap(set& swapset);
private:
struct *set;
now in the main we have set s1 and set s2, when I run swap like so: s1.swap(s2); s1 and s2 will swap the whole array and we need to keep the dimension of each array (so if s1 was set=new set[3] and s2 is set=new set[5]) after swap s1 is [5] and s2 is [3]
I was able to use insert and delete functions to swap the arrays when it was fixed dimension but I can't figure out how to change the dimension of the arrays during the swap function since the *set is private right?
thanks in advance for all the help!
edit: I added some parts of the code since I can't explain it correctly:
set::set()
:counter(0),m_size(0),flag(0)
{
m_set=new set[DEFAULT_MAX_ITEMS];
swapper=new set[DEFAULT_MAX_ITEMS];
maxsize=DEFAULT_MAX_ITEMS;
}
set::set(int x)
:counter(0),m_size(0),flag(0)
{
m_set=new set[x];
swapper=new set[x];
maxsize=x;
}
void set::swap(set& other)
{
// Exchange the contents of this set with the other one.
int tempmaxsize=maxsize;
int tempcounter=counter;
int tempmsize=m_size;
swapper=m_set;
m_set=other.m_set;
other.m_set=swapper;
m_size=other.m_size;
counter=other.counter;
maxsize=other.maxsize;
other.counter=tempcounter;
other.m_size=tempmsize;
other.maxsize=tempmaxsize;
}
private:
struct set
{
ItemType entry;
int count;
};
int maxsize;
set* m_set;
int m_size;
int counter;
int flag;
set* swapper;
error code is this:
debug assertion failed!
expression:_block_type_is_valid(phead->nblockuse)
Actually changing array sizes should be part of the swap function and so should be done in a class method. As it is a class method, it has access to private members.
My question is how to access and modify a 2D array defined in one class that is friends with another class. Below are some details on my question:
In class A I declare and allocate the appropriate space for my 2D array (pointer-to-pointer) u.
Class A
{
public:
friend class B;
long double **u;
int fun;
void make();
};
void A::make()
{
long double **u = new long double *[nx];
for (int i=0;i<nx;i++)
u[i] = new long double [ny];
int fun = 9;
}
Class A is friends with Class B; I need to use the array I declared in Class A in a function defined in class B. Below is my Class B:
class B
{
public:
void get(A*);
};
void B::get(A *pt)
{
using namespace std;
cout << pt->fun;
cout << pt->u[0][0];
}
I get a Bus error on my second cout pt->u[0][0]. Is there a simple way to use this setup I have to access my u[][] array? I think that I get the error because the pointer points to the 1st entry of my array, thus my whole 2D array is saved in memory as a single row (thinking aloud here). I'm a Fortran guy so this stuff is a little new to me.
Any help or "pointers" to other helpful threads would be appreciated.
Thank you !
Alberto
I think you get error because A::u is not initialized ( in method A::make you initialize a local variable u, not member. You need to change
void A::make()
{
long double **u = new long double *[nx]; // should be just u, or this->u.
There are some problems with your code: nx and ny don't seem to be defined anywhere, and in make you don't initialize A::fun at all, you instead set a local variable named fun which goes out of scope immediately.
As for your error, it sounds like the error stems from the fact that make() has not been called on pt. Ensure that make() is called on the instance you pass to get, otherwise the array u will not be allocated.