C# :: Declaring Objects Inside Constructor?
Jul 24, 2014
I notice I keep making the same mistake of not declaring certain objects, variables or lists inside the constructor so they e.g. a list populates when the view loads.
Which makes me think about it this way. Why do I need the constructor and when do I declare objects inside the constructor?
Maybe it just comes down to me not understanding the fundamentals of how to utilize the constructor.
View 5 Replies
ADVERTISEMENT
Apr 18, 2013
class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------
int numLights;
int lights[MAX_LIGHTS];
[Code] .....
I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.
How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.
View 6 Replies
View Related
Apr 6, 2012
I want to know
prog1.c
#include<stdio.c>
static int c=6;
int main() {
/*code*/
}
prog2.c
#include<stdio.h>
int main(){
static int c=10;
}
what would be the difference between these two program in the fuctioning of static keyword ?
View 1 Replies
View Related
Sep 12, 2013
work out this error?
guidedTour.cpp: In constructor 'GuidedTour::GuidedTour(std::string, std::string, double, double, Date, double, std::string)':
guidedTour.cpp:4: error: expected primary-expression before 'id'
guidedTour.cpp:4: error: expected primary-expression before 'description'
guidedTour.cpp:4: error: expected primary-expression before 'double'
guidedTour.cpp:4: error: expected primary-expression before 'double'
// SOURCE
#include "guidedTour.h"
[code]....
View 2 Replies
View Related
Nov 7, 2013
I've written an Array class to create 1d,2d and 3d array and it works fine for every test : example of the constructor of the array class for 2d case:
Array::Array( int xSize, int ySize ) {
xSize_ = xSize;
ySize_ = ySize;
zSize_ = 1;
vec.resize(xSize*ySize);
}
It works fine , but when i need to use this constructor inside of other constructor, i get the "no matching function error" ,
part of my code:
class StaggeredGrid {
public:
StaggeredGrid ( int xSize1, int ySize1, real dx, real dy ) : p_ (2,2) {}
[Code] .....
View 2 Replies
View Related
Mar 24, 2014
This keeps giving me the error
ecg.h:18:11: error: field "next" has incomplete type
How do I do what I need? It does the same thing whether I use a class or a struct. This is C++ code
struct ECG_node {
double voltage;
clock_t time;
ECG_node next;
[Code] .....
View 3 Replies
View Related
Oct 28, 2012
What the best way to initialize a constructor in an array of objects?
Usually I declare an array of objects like so:
Code:
Car toyota[100];
Is this the only way I know to initialize a constructor of an array:
Code:
for (int i = 0; i < 100; i++)
{
toyota[i] = Car(x, y);
}
Is this the proper way?
View 1 Replies
View Related
Nov 7, 2014
class Date
Date(int=1, int=1, int=1990);
class Person
Person(string="", string="", Date=NULL);
class RealEstateAgent:Public Person
RealEstateAgent(string="",string="",Date=NULL,Date=NULL,int=NULL, double=0.0);
}
[code]....
how can I assign default values with Customer object and RealEstateAgent?
View 4 Replies
View Related
Jan 6, 2015
Let's say I have a Car object , and it contains inner Engine object.
Code:
struct Car{
Engine mEngine;
};
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};
The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
View 5 Replies
View Related
Mar 1, 2014
i am writing this bank accounts program using structures. i haven't implemented the function before that i want to check if the data is being read and printed. When i build and run the program in visual studio it gives me the following error. "No constructor could take the source type, or constructor overload resolution was ambiguous". Now whats wrong in this program?
/* Bank Accounts Program */
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>//needed to use system() function
using namespace std;
const int MAX_NUM = 50;
struct Name{
[code]....
View 1 Replies
View Related
Jan 25, 2014
VS 2012 / Windows 7 64 bit
class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}
[Code] ....
it works until the myclass(int* p) calls myclass()
then the MyPointer will become CCCCCCCC (NULL value)!
is there a way to stop the second constructor from resetting the pointer value to null?
View 3 Replies
View Related
Nov 12, 2014
This has been bothering me for a while now, and I finally put together an example:
#include <iostream>
#include <string>
using namespace::std;
[Code]....
In the code above, the two classes hold pointers to each other, and that's fine but it doesn't seem right since C++ prefers to pass by reference. Yes, it can still do that (see testbox and testball) but even that seems odd to me because still you need to use pointer notation for the enclosed object. Am I the only one who feels this way, and should I just get over it? Or am I missing something that would allow an object to hold a reference?
View 4 Replies
View Related
Feb 28, 2012
I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:
error: type "mainClass" is not a direct base of "glavna"
This is the program I tried:
Code:
class mainClass {
private:
int x,y;
Code] ......
View 6 Replies
View Related
Jan 1, 2013
Is this example correct? This example from a book
Constructor of the Base Class
Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
name = n;
nationality = nat;
sex = s;
}
Constructor of the Derived Class (inherited from the base class)
Student(char* n, int s=0, char* i=""):
Person(n, s)
Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?
View 5 Replies
View Related
Nov 6, 2014
I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.
Code:
#include <iostream>
#include "Calculate.h"
#include "Vehicle.h"
#include <fstream>
Calculate::Calculate(){
[Code] ....
View 9 Replies
View Related
Jun 5, 2013
I am a bit confused about how specific one must be with arguments when declaring a function. I'll show you two functions from the book I'm using to learn C to show you what I mean.
Example 1 (greatest common denominator function):
Code:
void gcd (int u, int v) {
int temp;
printf ( "
[Code] ....
So in that function, there are exactly two arguments, because that's how many arguments the algorithm to find the gcd takes. No problem there, makes sense to me. Then further in the chapter on functions I run into this,
Example 2 (square root function):
Code:
float absoluteValue (float x) {
if ( x < 0 )
x = -x;
return x;
[Code] ....
In this second example, we have a square root function that is preceded by an absolute value function. The absolute value function has the one argument, "float x", however when this function is called within the square root function, the arguments "guess * guess * -x" are passed to it. I'm confused how this absolute value function is working with all of that inside it, when it was originally declared with just "x." The only possibility I can think of is that this expression is treated as a single unit, but I'm not sure.
View 2 Replies
View Related
Apr 4, 2013
Code:
if (IS_LEAP_YEAR(year))
const int days_per_month[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
const int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Is it ok to declare the array in this manner or is it bad?
And i have to ask the user for a date to enter in my program. So should I use scanf or should I store the date in a string and then use sscanf. I have to check for valid input for everything like day, month, year etc. I did it as below..
Code:
int assignments;
assignments = scanf("%d / %d / %d", &month, &day, &year);
fflush(stdin);
if (assignments != 3)
{
printf("Retry: ");
}
else
error checking.
View 9 Replies
View Related
Aug 1, 2013
Code:
#include <stdio.h>
int main(void){
int a=0;
for(;a<=10;)
int b;
return 0;
}
I have got a code like this. I don't expect to get an output but just assumed I would see the command screen until I terminated it. What I want to do is just declare a variable b in a endless loop. But what I got from the compiler is this error: error: expected expression before 'int'. I am using Code::Blocks and I think the compiler is GCC.
View 4 Replies
View Related
Jun 13, 2014
I'm using some rather large external libraries, and I want to load them in my .cpp file only. so, my header looks like this:
namespace {
// hidden declarations
namespace geometry {
class Point;
class Polygon;
class Box;
//etc
}
}
In the declaration of the main class in that header, I merely use these as pointers or references. The .cpp file looks as follows:
// Boost
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
[Code] .....
This doesn't work however:
error C2371: '`anonymous-namespace'::geometry::Point' : redefinition; different basic types
1> e:..........NavigationMesh.h(10) : see declaration of '`anonymous-namespace'::geometry::Point'
What can I do to forward declare external classes like this?
View 13 Replies
View Related
Jun 30, 2014
We have been assigned to create an iTunes library. Everything compiles in my other .h file but my main is not happy with my object declaration. It keeps stating "primary expression before '{'". Here is my main code:
#include<iostream>
#include<string>
#include<fstream>
#include"myTunes.h"
using namespace std;
//function protocols
void read(string);
[Code] ......
View 1 Replies
View Related
Mar 18, 2013
I'm writing this program that basically interprets the rottentomatoes website. I am however having a problem declaring if it is rotten or fresh according to the rating the user enters.
I'm outputting it here:
void PrintAll(const string titles[], const int ratings[], int count) {
WriteLine('=', 50);
cout << "PRINT ALL" << endl;
WriteLine('-', 50);
[Code] .....
And here is my condition:
string RatingToString(const int ratings[], int count) {
string rank;
for(int i = 0; i < count; i++) {
[Code]....
Here is the output:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
==================================================
MENU
1. Add Movie
2. Print All
3. Exit
--------------------------------------------------
Enter 1-3 : 1
Title : Hitch
Rating : 90
==================================================
[Code]....
My condition works when there is only one movie, but when I add more, it gives it the new movies "ROTTEN" or "FRESH" rank.
View 1 Replies
View Related
Sep 26, 2014
Is it permissible to declare, for example, `std::valarray<int&>`? If so, how do I initialize such if the `valarray` is a class member?
View 3 Replies
View Related
Oct 29, 2013
I have the following code:
struct mystruct {
char fieldA[5];
char fieldB[7];
};
void dosomething(struct mystruct* pms) {
[Code] ....
Is there any problem doing that, even for a C89/90 compiler?
View 3 Replies
View Related
Oct 21, 2012
I can not set the size of my array while running porgrama. Is there any way to do this in C + +?
---- code ------
#include <iostream>
#include <string>
using namespace std;
[Code].....
View 5 Replies
View Related
Sep 9, 2014
see these sample:
Code:
#include <iostream>
#include <string>
using namespace std;
class test{
public:
virtual void ola() {
[Code] .....
Like you see, i don't re-declare the 'ola' function in 'test1' class, only in 'test' class. The compiler tell me the 'ola' isn't member of 'test1'. in 'test' i put it 'virtual', but forgetting that, how can i override it without re-declare it?
View 10 Replies
View Related
Nov 12, 2012
This is a c program that is failing to compile. The error occurs in the calcLabs() function. The error called out is (btw, I'm using VS 2010): Error4error C2143: syntax error : missing ';' before 'type'
I don't understand why the compiler is not letting me declare variables in the calcLabs() function!
Code:
#include<stdio.h>
#include<stdlib.h>
void calcPercent(double *);
double calcLabs();
double calcExams();
double calcFinal();
char calcLetter(float);
[code]...
View 5 Replies
View Related