C++ :: Default Constructor - Set Array Of Strings To Null And Size Variable To 0

Dec 7, 2013

For my default constructor I need to set an array of strings to null and the size variable to 0. Here is my code for that

DynamicStringArray::DynamicStringArray() {
*DynamicArray[]=NULL;
size=0;
}

When I try and compile it I get an "expected primary expression before ']' token" error.

View 1 Replies


ADVERTISEMENT

C/C++ :: Print Array Of Strings Is Printing Null Instead Of Result

Feb 17, 2014

I am trying to print the array of string using this code but the compiler is printing null in the output:

The code is as follows:

char *arr[10];
int i;
for(i=0;i<10;i++)
scanf("%s",*(arr+i));  
for(i=0;i<10;i++)
printf("%s
",*(arr+i));

View 3 Replies View Related

C++ :: Calling Constructor From Constructor Set Pointer To Null

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

C++ :: Set Array Size At Compile Time Through Constructor

Oct 22, 2013

How do I set the size of a member array via the class constructor if I know the size at compile time. I can do this with templates, see below, but this leads to code bloats, I think.

So one class declaration but objects with different array sizes.

Can't use constexpr.
Can't use STL.
Can't use new.

#include <iostream>
using namespace std;
template<int T>
class MyArray {
private:
int array[T];
public:
int getSize()

[Code] ....

View 15 Replies View Related

C++ :: Storing Variable Size Pointers Array As Class Variable

Mar 2, 2013

This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.

#include <iostream>
using namespace std;
class A {
public:
int ** pointer;

[Code] ....

I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.

View 6 Replies View Related

C++ :: No Appropriate Default Constructor Available?

Oct 12, 2014

First I wrote a Binary-tree class to draw a binary tree on the window. The nodes were small circles. Then I become wanted to change the shape of the nodes from circles to triangles by another class, Binary-tree-derived which is derived from Binary-tree class. I wrote the below code to do the job but I get two errors about constructors. First, code:

/* The binary-tree class, one of the topics that has been said in Programming Principles and Practice using C++ book by Biarne Stroustrup.
This code is written by R Abbasi (s.rabbasi@yahoo.com) */
#include <Simple_window.h>

[Code].....

Errors are:

Error12error C2512: 'Binary_tree' : no appropriate default constructor availablec:userscsdocumentsvisual studio 2012projects est_1 est_1 est_1.cpp91

13IntelliSense: no default constructor exists for class "Binary_tree"c:UsersCSDocumentsVisual Studio 2012Projects est_1 est_1 est_1.cpp91

View 4 Replies View Related

C++ ::  How To Write A Default Constructor

Sep 27, 2013

How do you write a default constructor?I need to use a default constructor that will initialize the data members:
- salesPerson to “Unknown”
- noOfWeek to 13
- amount to point to an array of thirteen 0.00s.

This is my weeklysales class

class WeeklySales {
char* salesPerson;
double* amount; // Pointer to an array
int noOfWeek; // Size of the array
};

Here's my attempted code:

//default constructor
WeeklySales (){
salesPerson="Unknown";
noOfWeek = 13;
amount = 0.00;
}

View 9 Replies View Related

C++ :: Synthesized Default Constructor

Dec 25, 2013

A compiler auto created default constructor is called a synthesized default constructor. It will initialize the built-in members to 0 or not depends on where the class object is defined? if I define a class

class point{
public:
double x, y;
};

if I define point point1; in global scope then point1.x and point1.y will be initialized to 0, if I define point point2; in a local scope, then its x and y won't be initialized? If it is like this, then I believe if there are built-in type members in a class, then the synthesized default constructor is almost useless!

View 3 Replies View Related

C++ :: Creating Object Without Default Constructor

Jun 24, 2013

I have a question about the default constructor.

To my best understanding, the compiler will provide me with a deafult constructor only if there are no any user defined constructors, at all. Now consider the following code:

Code: class MyClass
{
private:
int m_data;
public:
MyClass(int init):m_data(init){cout<<"Ctr called"<<endl;}

[Code] ....

How is it that suddenly, there is a default constructor?

View 3 Replies View Related

C++ :: Undefined Reference To Default Constructor?

Jul 28, 2013

trying to practice the object-oriented part of it by converting my java programs into c++. I believe I understand the concepts of a header file and declaring the functions in the .cpp files. I keep getting this "Undefined reference to NamedStorm::NamedStorm()" error.

NamedStorm.h
#ifndef NAMEDSTORM_H
#define NAMEDSTORM_H
#include <string>
#include <iostream>
// NEVER use using namespce in header, use std instead.
using std::string;

[code]....

View 7 Replies View Related

C++ :: Create Two Box In Program Using Default Constructor

Apr 6, 2014

Im trying to create two box in this program using the default constructor. When i call to try and display the info, it says that x, y, and z are not declared in this scope. i wanted to have the user cin the length, height, and width using the void setBox function.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class Box{
public:

[code]....

View 2 Replies View Related

C/C++ :: Use Of Implicit Default Constructor Provided By Compiler?

Sep 21, 2014

In C++ if no user defined constructor is provided for a class the compiler automatically provides a implicit default constructor.

An implicit default constructor is equivalent to a default constructor with no parameters and no body ?

So what is the use of a implicit default constructor provided by the compiler, if it is not going to do anything ?

View 3 Replies View Related

C++ :: Call To Implicitly - Deleted Default Constructor Of Node

Oct 26, 2014

i tried running my code and i keep getting the error "call to implicitly-deleted default constructor of 'node'. My code is as follows

//.h file
typedef struct node * point
struct node{
string data;
node *next
}

[code]....

I get the error at the line "ptr1 = new node;" I tried putting a default constructor for my node struct and that fixed the problem but a new problem arises. It states that i have a linker error after i compile it with a default constructor.

View 7 Replies View Related

C++ :: Make A Default Constructor Having Other Class Member Objects

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

C/C++ :: How To Define Default Constructor For A Template List Class

Apr 18, 2012

I was making a template list class, and using it to make list of objects of my own class.It works fine with integers, but not with other classses.

template <typename T>
class CList {
public:
    struct Node {
        T data;
        Node* next;

[code]....

While in AddElement(), it gives error - Default constructor not available.

template<typename T>
bool CList<T>::AddElement(T t) {
    bool result = true;  
    if (head == NULL) {

[code]....

what is wrong here.

View 1 Replies View Related

C++ :: How To Insert Array Size As Variable

Nov 30, 2013

How do I insert a array size as variable?

View 5 Replies View Related

C++ :: What Is Default Size Of STL Vector

Jan 30, 2012

For example, I have an empty vector of integer. If I keep calling push_back on vector, is it going to be out of memory?

View 8 Replies View Related

C :: Size Of Struct With Variable Length Array?

Mar 6, 2015

The WinAPI has a struct like this for raw input:

Code:

typedef struct tagRAWINPUT { RAWINPUTHEADER header;
union {
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
} data;

[code]...

The definition of the struct doesn't show it but the documentation says that bRawData is variable length. sizeof(RAWINPUT) will not be the correct size when the data field is of RAWHID type so how do you allocate a variable with automatic storage type that has the right size for the entire struct? You can get a header that has the size for the entire struct but how do you actually allocate storage space for the data without using malloc? I've seen some example code that used a char array but that violates aliasing rules and there are also alignment issues with that approach.

View 5 Replies View Related

C :: Make Array Which Contains Arrays But With A Variable Size

Jan 29, 2015

I am wondering how I could make an array which contains arrays, but with a variable size.My first try..

Code:

int array[][] = {{1}, {2}};

But this isn't proper

View 2 Replies View Related

C++ :: Static Array Variable Size Allowed?

Mar 1, 2013

I have a function like this

void foo( int i) {
...
uint8_t buf[ i];
...
}

And I don't understand why the compiler is not complaining... I'm using g++ -c -g -Wall to compile ....

View 2 Replies View Related

C++ :: Dynamic Array Of Variable-length Strings

Oct 10, 2013

I believe that everything is fine except that I can think of the condition can be inserted inside the parentheses ..

#include <iostream>
using namespace std;
int main() {
int i=0, k=0;
char *string_n, **matrix, temp;

[code].....

View 2 Replies View Related

C :: Declaring Array Size Using Variable Causes Program To Crash

May 10, 2013

I am new to C. I've been trying to use C to code some statistical functions originally coded in R. I've encountered an interesting phenomenon. In the function foo1, I declared the array v1v2b using an actual value 1999000. The function runs fine when I call it in R.

Code:
void foo1(double *x, double *y, int *nsamp){
int i, j, k, oper=2, l;
double* v1v2=malloc(sizeof(double)*((*nsamp)*(*nsamp-1)/2 + 1));
outer_pos(x, y, nsamp, &v1v2[0]);
double v1v2b[1999000]; //<-------HERE
for(i=1; i<= 1999000]; i++){
v1v2b[i-1]=1;
} }

However, in foo2, I first create an integer variable called index, and store the value 1999000 in it. I then use it to initialize the same array. When I tried calling this function in R, it either led to a stack overflow error, or completely crashed R.

Code:
void foo2(double *x, double *y, int *nsamp){
int i, j, k, oper=2, l;
double* v1v2=malloc(sizeof(double)*((*nsamp)*(*nsamp-1)/2 + 1));

[Code] .....

View 9 Replies View Related

C++ :: Insert Variable For Size Of Array And Prompt User To Insert Elements

Dec 1, 2013

how to insert a variable for the size of array and prompt user to insert the elements for the array?

View 13 Replies View Related

C/C++ :: Limiting Size Of Strings To 5?

Feb 4, 2015

I am supposed to use scanf() for this assignment to limit the number of charachters in the string. However, I cant seem to get it to work in either scanf() or fgets().

// main.c
// Lab_Ccode_1 + extra credit = input validation.
//
// Created by IanMiles on 1/28/15.

[Code]....

View 6 Replies View Related

C++ ::  How To Pass Variable To A Function In Constructor

Jun 11, 2014

In a project I am working on, I have to initialize a window and pass it as a parameter to another constructor, but before the window is initialized, it is passed as a parameter thus causing an error. Here is some code :

Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mWorld(mWindow) //<---- right here is where the mWindow variable needs to be passed
{
//...
}

Is there a way to make this work???

View 2 Replies View Related

C++ :: Limit Size Of String (arrays And C-style Strings)

Dec 9, 2014

how do I tell the if statement to output this error message 'exceeded the maximum amount of characters' that has its characters stored in an array using c-style string?

[INPUT] The cat caught the mouse!
[OUTPUT] Exceeded the maximum amount of characters (max 10)
#include<iostream>
#include<string>

[Code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved