-1

I am trying to create the geometry Cylinder using cpp. The job is to create a cylinder geometry on the x-y plane. I am using gcc -o c.exe cylinder.c in command prompt to make an output c.exe from the cylinder.c input c file. I am using a code that I found on the internet for the .c file. The code is:

// generate a unit circle on XY-plane
std::vector<float> Cylinder::getUnitCircleVertices()
{
    const float PI = 3.1415926f;
    float sectorStep = 2 * PI / sectorCount;
    float sectorAngle;  // radian

    std::vector<float> unitCircleVertices;
    for(int i = 0; i <= sectorCount; ++i)
    {
        sectorAngle = i * sectorStep;
        unitCircleVertices.push_back(cos(sectorAngle)); // x
        unitCircleVertices.push_back(sin(sectorAngle)); // y
        unitCircleVertices.push_back(0);                // z
    }
    return unitCircleVertices;
}
...

// generate vertices for a cylinder
void Cylinder::buildVerticesSmooth()
{
    // clear memory of prev arrays
    std::vector<float>().swap(vertices);
    std::vector<float>().swap(normals);
    std::vector<float>().swap(texCoords);

    // get unit circle vectors on XY-plane
    std::vector<float> unitVertices = getUnitCircleVertices();

    // put side vertices to arrays
    for(int i = 0; i < 2; ++i)
    {
        float h = -height / 2.0f + i * height;           // z value; -h/2 to h/2
        float t = 1.0f - i;                              // vertical tex coord; 1 to 0

        for(int j = 0, k = 0; j <= sectorCount; ++j, k += 3)
        {
            float ux = unitVertices[k];
            float uy = unitVertices[k+1];
            float uz = unitVertices[k+2];
            // position vector
            vertices.push_back(ux * radius);             // vx
            vertices.push_back(uy * radius);             // vy
            vertices.push_back(h);                       // vz
            // normal vector
            normals.push_back(ux);                       // nx
            normals.push_back(uy);                       // ny
            normals.push_back(uz);                       // nz
            // texture coordinate
            texCoords.push_back((float)j / sectorCount); // s
            texCoords.push_back(t);                      // t
        }
    }

    // the starting index for the base/top surface
    //NOTE: it is used for generating indices later
    int baseCenterIndex = (int)vertices.size() / 3;
    int topCenterIndex = baseCenterIndex + sectorCount + 1; // include center vertex

    // put base and top vertices to arrays
    for(int i = 0; i < 2; ++i)
    {
        float h = -height / 2.0f + i * height;           // z value; -h/2 to h/2
        float nz = -1 + i * 2;                           // z value of normal; -1 to 1

        // center point
        vertices.push_back(0);     vertices.push_back(0);     vertices.push_back(h);
        normals.push_back(0);      normals.push_back(0);      normals.push_back(nz);
        texCoords.push_back(0.5f); texCoords.push_back(0.5f);

        for(int j = 0, k = 0; j < sectorCount; ++j, k += 3)
        {
            float ux = unitVertices[k];
            float uy = unitVertices[k+1];
            // position vector
            vertices.push_back(ux * radius);             // vx
            vertices.push_back(uy * radius);             // vy
            vertices.push_back(h);                       // vz
            // normal vector
            normals.push_back(ux);                       // nx
            normals.push_back(uy);                       // ny
            normals.push_back(nz);                       // nz
            // texture coordinate
            texCoords.push_back(-ux * 0.5f + 0.5f);      // s
            texCoords.push_back(-uy * 0.5f + 0.5f);      // t
        }
    }
}

This gives me some errors:

cylinder.c:2:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
 std::vector<float> Cylinder::getUnitCircleVertices()
    ^
cylinder.c:18:1: error: expected identifier or '(' before '...' token
 ...
 ^

I guess there is a ';' and a circular bracket missing somewhere that I cannot find. Can anyone help me with the problems?

  • 5
    Did you `#include `? – NathanOliver Jan 30 '20 at 13:51
  • 9
    C++ code should have a `.cpp` or similar extension, your compiler is probably assuming your code is c – Alan Birtles Jan 30 '20 at 13:51
  • Besides naming your source file with a `.cpp` suffix, also use the `g++` front-end program (and not `gcc`), as this will automatically set up header-file paths for the C++ standard library and link with the C++ library. – Some programmer dude Jan 30 '20 at 13:52
  • Clearly not #including the error occurs on line 2. That is the error. Just a missing include. And g++ – Jeffrey Jan 30 '20 at 13:53
  • You can force `gcc` to compile C++ by using `gcc -xc++ -lstdc++ -shared-libgcc`, but as a courtesy to anyone who is compiling this after you I suggest changing names to `*.cpp` and using `g++` command to compile. – Yksisarvinen Jan 30 '20 at 13:54
  • You should remove the line with `...`, this is not valid code. – VLL Jan 30 '20 at 13:55

2 Answers2

0

use cc or cpp file extension; your compiler tries to compile it as a c code.

nivpeled
  • 1,810
  • 5
  • 17
0

You should use the .cpp (or similar) extension for c++ code. .c usually indicates C code. You also need to use g++ rather than gcc to compile your code otherwise again it will be treated as C code.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60