0

The header file will not compile into my main test program. Why would this be the case. I have looked on line but found no simple reason why this would be the case. I have tried a couple of #ifndef, and #define and am still not sure as to why the file will not include into my test program.

Below are the error message that I receive when I try and compile my test program. This has to do with the header file but I am not really sure how to fix this simple problem. Strangely i have used C++ before and did not remember having this trouble with the header file.

error:

Error 1 error C2015: too many characters in constant c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp 10 1 TestProject

Error 2 error C2006: '#include' : expected a filename, found 'constant' c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp 10 1 TestProject

Error 3 error C1083: Cannot open include file: '': No such file or directory c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp 10 1 TestProject

code

#include "stdafx.h"
#include "iostream"
#include <iostream>
#include <fstream>
#include <math.h>
#include <iostream>

#ifndef MYDATESTRUCTURES_H
#define MYDATESTRUCTURES_H
    #include'myDataStructures.h' <-- name of my include file
#endif

using namespace std;

#define MY_NAME "Alex"

void f(int);
void DoSome(int, char);

enum color  { red, green, blue };
enum color2 { r, g=5, b };

class CVector {
  public:
    int x,y;
    CVector () {}
    CVector (int a, int b) : x(a), y(b) {}

    void printVector()
    {
        std::cout << "X--> " << x << std::endl;
        std::cout << "Y--> " << y << std::endl;
    }
};

CVector operator+ (const CVector& lhs, const CVector& rhs) {
  CVector temp;
  temp.x = lhs.x + rhs.x;
  temp.y = lhs.y + rhs.y;
  return temp;
}

template<typename T>
void f(T s)
{
    std::cout << s << '\n';
}

template<typename P, typename N>
void DoSome(P a, N b)
{
     std::cout << "P--> " << a << '\n';
     std::cout << "N--> " << b << '\n';
}

void testMath()
{
    int result = ceil(2.3) - cos(.2) + sin(8.0) + abs(3.44);
    cos(4.1);
}

void testStorageTypes() 
{
   int a;
   register int b;
   extern int c;
   static int y;
}

color temp = blue;
color2 temp2 = r;

int _tmain(int argc, _TCHAR* argv[])
{
    std::getchar();
    return 0;
}

code (header file)

#include <iostream>

int myAdd1(int, int);
int myAdd2(int, int, int, int, int);

struct myFirst1
{
}

struct myFirst2
{
}

int myAdd1(int x, int y)
{
    return x + y;
}

int myAdd2(int x, int y, int z, int m, int y)
{
    return x + y;
}
Community
  • 1
  • 1

4 Answers4

8

This line isn't valid:

#include'myDataStructures.h' <-- name of my include file

In C/C++, single quotes are used to quote character literals, not string literals. You need to use double quotes:

#include "myDataStructures.h"

The error messages are slightly less helpful than they could be because it is actually possible to have a multi-character constant, but its value is implementation-defined, making their use not very portable and therefore rare.

Community
  • 1
  • 1
Jason R
  • 11,159
  • 6
  • 50
  • 81
2

You need either to give the file names enclosed with double quotes ("include_filename"), or angle brackets (<include_filename>) with the #include statement:

 #include "myDataStructures.h"

Using double quotes looks in additional directories given via the compiler's -I (for e.g. GCC) or equivalent option (including the issuing files directory), before falling back to angle bracket search order (that also looks up directories intrinsic for the current toolchain).

Another IMPORTANT point about your (fixed) sample:

#ifndef MYDATESTRUCTURES_H
#define MYDATESTRUCTURES_H
    #include "myDataStructures.h"
#endif

these preprocessor conditionals should be placed inside your myDataStructures.h file, not where you are including it:

myDataStructures.h

#ifndef MYDATESTRUCTURES_H
#define MYDATESTRUCTURES_H

#include <iostream>

int myAdd1(int, int);
int myAdd2(int, int, int, int, int);

// ...

#endif

These include guards are meant to avoid 'multiple declaration/definition' compiler errors if the myDataStructures.h file is included multiple times from different header files. It will prevent rendering the code again, if it was seen once by the preprocessor.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

You need to use double quotes "MyIncludeFile.h" when including files.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
abousquet
  • 576
  • 3
  • 13
0

Use #include "myDataStructures.h" instead of #include 'myDataStructures.h'

The latter causes your syntax error.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69