Skip to content
C++ Better Explained
Go back
How to Fix 'type name is not allowed' in C++ (Visual Studio)

How to Fix “type name is not allowed” in C++ (Visual Studio)

Short answer: you wrote the name of a type, such as int, std::string or one of your own classes, where C++ needs a value. A type only describes what kind of thing something is; you can’t pass it, print it, or call a function on it. Use a variable or object of that type instead, or :: for something that belongs to the class itself.

WhereWhat you see
Visual Studio editor (red squiggle)type name is not allowed
Visual Studio buildC2275: expected an expression instead of a type
GCC (g++, MinGW, Code::Blocks)expected primary-expression before '.' token
Clang (Xcode, macOS)'Point' does not refer to a value

Hit “type name is not allowed”? It's a classic beginner trap, and there are others like it.

Free 1-page checklist: The 10 Mistakes Every C++ Beginner Makes. It shows the exact traps that slow down every C++ beginner, so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Type vs Value

int is a type. 4 is a value of that type, and so is a variable like int x = 4;. In the same way, std::string is a type and std::string name = "Sam"; gives you a value called name.

A cookie cutter and a cookie is a good way to picture it: the class or struct is the cutter, and each object is a cookie made with it. You can eat a cookie, but not the cutter. This error means you tried to use the cutter.

Cause 1: A Type in a Function Call

Copying a function’s declaration into the call is very common:

#include <iostream>
#include <string>

int square(int n) {
    return n * n;
}

int main() {
    int x = 4;
    int result = square(int x);
    std::cout << result << '\n';
}
Visual Studio editor:  type name is not allowed
MSVC build:            error C2144: syntax error: 'int' should be preceded by ')'
GCC:                   error: expected primary-expression before 'int'
Clang:                 error: expected '(' for function-style cast or type construction

The type belongs in the function’s definition (int square(int n)). When you call it, you pass just the value. Fix: square(x), which prints 16.

Cause 2: Using a Struct Instead of an Object

#include <iostream>
#include <string>

struct Point {
    int x;
    int y;
};

int main() {
    Point p = {3, 4};
    std::cout << Point.x << '\n';
}
Visual Studio editor:  type name is not allowed
MSVC build:            error C2275: 'Point': expected an expression instead of a type
GCC:                   error: expected primary-expression before '.' token
Clang:                 error: 'Point' does not refer to a value

Point is the struct; p is the actual point. Fix: use the object’s name: std::cout << p.x << '\n'; prints 3. See structs explained for more.

Classes and inheritance feeling abstract? C++ Better Explained explains object-oriented C++ in plain English, with analogies and code diagrams, from your first class to inheritance and polymorphism.

Get the book — $27

Cause 3: Calling a Static Function With a Dot

#include <iostream>
#include <string>

class Counter {
public:
    static int count() { return 3; }
};

int main() {
    std::cout << Counter.count() << '\n';
}
Visual Studio editor:  type name is not allowed
MSVC build:            error C2275: 'Counter': expected an expression instead of a type
GCC:                   error: expected primary-expression before '.' token
Clang:                 error: 'Counter' does not refer to a value

A static function belongs to the class, not to any one object, so you call it through the class with ::. Fix: Counter::count(), which prints 3. The dot is only for objects. More on this in the static keyword and the scope resolution operator.

Cause 4: std::string.size() Instead of name.size()

#include <iostream>
#include <string>

int main() {
    std::string name = "Sam";
    std::cout << std::string.size() << '\n';
}
Visual Studio editor:  type name is not allowed
MSVC build:            error C2275: 'std::string': expected an expression instead of a type
GCC:                   error: expected primary-expression before '.' token
Clang:                 error: expected '(' for function-style cast or type construction

size() tells you the length of a particular string, so it needs the string: Fix: name.size(), which prints 3. The same goes for vectors: scores.size(), not std::vector<int>.size().

Cause 5: Printing a Type

std::cout << int; gets the same squiggle. There’s nothing to print: a type has no value. To print a variable, use its name. To see how big a type is, use sizeof(int), which is allowed because sizeof takes a type in brackets. See the sizeof operator.

Quick Reference

You wroteFix
square(int x)square(x)
Point.xp.x (an object of type Point)
Counter.count() for a static functionCounter::count()
std::string.size()name.size()
std::cout << int;Print a variable, or sizeof(int)

For other common messages, see C++ error messages explained.


Take Your C++ Further

C++ Better Explained ebook cover

Object-oriented C++ clicks once you have a clear picture of what an object is and how classes relate to each other. C++ Better Explained covers classes, inheritance and polymorphism in plain English, with analogies and code diagrams.

Get the book — $27

87 pages · plain English · 30-day money-back guarantee


Next common error How to Fix 'expected primary-expression before' in C++
📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Fix "expected a ')'" in C++ (Visual Studio)
Next Post
How to Fix 'expression must have a constant value' in C++

Keep Learning