Skip to content

Posts from the ‘C and C++’ Category

19
Jul

Error: main.cpp: warning: using directive refers to implicitly-defined namespace ‘std’

If you see the following error when trying to compile a C++ application:

main.cpp: : : warning: using directive refers to implicitly-defined namespace 'std'

then that means you do not have any header file inclusion that uses std namespace.

You can fix this warning by including a C++ header file that uses a std namespace otherwise the compile will not know about std namespace.

18
Jul

Example: Implement a Stack of Strings in C++

Next example show how to implement a class of strings in C++. We will first read a text from a file and we will store it into a C++ string. Then we will split the string into words (by spaces) and we will store the words in a vector of strings.

Then we will iterate through that vector of strings and we will add elements our stack only if element not equal with “-“. If element is equal with “-” character then we will pop element from top of stack. We will display elements that we remove (using pop()) from our stack.

Class ReadFromFile

Class ReadFromFile header file (ReadFromFile.h):

#include <iostream>
#include <fstream>
 
using namespace std;
 
class ReadFromFile {
private:
    string fileName;
    long long buffLength;
    string buff;
 
public:
    ReadFromFile(string file);
    ~ReadFromFile();
 
    string displayBuffer();
    long long getFileLength();
};
</fstream></iostream>

Read more »

18
Jul

Split a String Into Words Using a Vector of Strings and Iterate through it in C++

Next c++ example shows how to split a string into words by namespaces (or end of line character), store it into a vector of strings and then iterate throug it (mai.cpp).

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
 
using namespace std;
 
int main(int argc, const char * argv[]) {
 
    string str1("the brown fox jumped over the lazy dog");
    vector<string> vector1;
    istringstream iss(str1);
 
    while (iss) {
        string word;
        iss >> word;
        //cout << word << endl;
        vector1.push_back(word);
    }
 
    for (int i; i < vector1.size(); i++) {
        cout << vector1[i] << endl;
    }
 
    return 0;
}
18
Jul

Create a Vector of Strings and Iterate Through it in C++

In the next example we will create a vector of strings and will iterate through it (main.cpp).

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int main(int argc, const char * argv[]) {
 
    string str1[] = {"the", "brown", "fox", "jumped", "over", "the", "lazy", "dog"};
    vector<string> vector1(str1, end(str1));
 
    for (int i; i < vector1.size(); i++) {
        cout << vector1[i] << endl;
    }
    return 0;
}
18
Jul

Split a C++ String Into Words by Spaces or End of Lines

Next example shows you how to split a string into multiple words delimited by spaces or end of line (main.cpp).

#include <iostream>
#include <string>
#include <sstream>
 
using namespace std;
 
int main(int argc, const char * argv[]) {
 
    string str1("the brown fox jumped over the lazy dog");
    istringstream iss(str1);
 
    while (iss) {
        string word;
        iss >> word;
        cout << word << endl;
    }
    return 0;
}
17
Jul

C++ Example: A Class to Read a File

Here is a C++ example of a class to read content of a text file and store it in a buffer (char *). Everything is done in class’s constructor.

The header file (ReadFromFile.h):

#include <iostream>
#include <fstream>
 
using namespace std;
 
class ReadFromFile {
private:
    string fileName;
    long long buffLength;
    char *buff;
 
public:
    ReadFromFile(string file);
    ~ReadFromFile();
 
    char* displayBuffer();
    long long getFileLength();
};
</fstream></iostream>

Read more »

6
Mar

no matching function for call to ‘glutinit’

If you get this error in Xcode:

no matching function for call to ‘glutinit’

when you are trying to write a simple program with Glut a quick fix is to remove “const” from:

int main(int argc, const char * argv[])

23
Feb

duplicate symbol MyFunction in xcode

If you get the following error when trying to compile a C (or Objective C) program in XCode:

duplicate symbol MyFunction

(null): 1 duplicate symbol for architecture x86_64

then the problem is that you’ve included a .c file using #include directive or #import directive instead of including the .h file.

If you’ve created some functions in a separate .c file (or .m file) don’t just import that file in your main file but create a header file too and move header definitions to that file.

16
Feb

How To Allocate Memory in C With malloc()

If you use pointers in C you must first allocate memory, before you will use the pointer.

To allocate memory in C we will use malloc() function which is defined in header. To free memory allocated with malloc() we will use free() function which is also located in header.

Example 1

See next example:

#include <stdio.h>
#include <stdlib.h>	// for malloc() and free() functions
 
#include <string.h>
 
int main(void) {
 
    char str1[9] = "a string";
 
    char *str2 = 0;	// it's safer to make str2 = 0
                        // which is usefull if we forget
                        // to alloc memory before we will
                        // use it
 
    str2 = (char*)malloc(9);	// will alocate memory
 
    strcpy(str2, str1);		// will copy str1 into str2
 
    printf("str1 value is = %s\n", str1);
 
    printf("str2 value is = %s\n", str2);
 
    free(str2);		// we deallocate memory allocated
                        // for str2
}

Read more »

10
Jul

Quick Guide to Object Oriented Programming in C++ (Part 2)

This is a second part from a quick guide to Object Oriented Programming in C++ (aka OOP). We want this guide to be a very practical intro to Object Oriented Programming in C++.

In this tutorial we will write some examples that describe make use of encapsulation, inheritance. and polymorphism

1. Encapsulation

Encapsulation is the process of hiding implementation informations of a class or function so that only needed information will be available to the user of the class. This way we protect the the class/library against directly accessing/modifying values of some variables of the class for example that should have some values only. For example if we have a variable in a class that keep Score and we do not want the minimum and maximum score to be in a range we can hide that variable and only allow Setters and Getters functions to access it. A Setter function is a member function of that class that we will define, it’s a function that have access to that variable and set the value for that variable. We cannot access that variable directly but that member function can. And that Setter function (we call it Setter function because it sets a value for that variable) will know to set score within a range and will take care of wrong values added by user of the class.
Read more »