Hi, I'm doing a very very simple coding, However the compiler apparently does not like my usage of cin and gives me an error: no match for 'operator<<' or 'operator>>' depending on cout or cin.

I've used this many times before but never have encountered this problem trying to use << or >> operators. I suppose I could try to overload it but I shouldn't have to.

#include <stdio.h>
#include <math.h>
#include <iostream.h> 
#include <stdlib.h>

using namespace std;

void calcDCT(int num, float * item)
{
	float alpha;
	float coS;
	float sum;
	int intensity;
	double PI = 3.14;
	float * dct = new float [num];

	for(int i = 0; i < num ; i++)
	{
		if (i==0)
			alpha = 1/sqrt(2);
		else
			alpha = 1;
		sum = 0;
		for(int j = 0; j < num; j++)
		{
			if (j==0)
				alpha = 1/sqrt(2);
			else
				alpha = 1;
			coS = cos(j*PI*(2*i+1)/(2*num));
			intensity = dct[j];
			sum += sqrt(2/num)*alpha*coS*intensity;
		}
		dct[i] = sum;
	}
	
	for(int i = 0; i < num; i++)
	{
		cout << dct[i] << endl;
	}
}

int main() 
{
	int num;
	float temp;
	
	cout << "Enter N: ";
	cin >> num >> endl;
	float * item = new float [num];

	for(int i = 0; i < num; i++)
	{
		cout << "Enter intensity value " << i << ": " << endl;
		cin >> temp >> endl;
		item[i] = temp;
	}

	calcDCT(num, item);

	return 0;
}

cin >> temp >> endl; part gives me a nasty error test.cpp:57: error: no match for ‘operator>>’ in ‘std::cin.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((int&)(& num))) >> std::endl’ Any input would be deeply appreciated.

Dani AI

Generated

The error arises because std::endl is an output manipulator. It only works with operator<< on output streams. Using it with operator>> on std::cin has no valid overload, so you see "no match for operator>> ... std::endl". If you want a newline after prompting, write it to std::cout or use '\n'. If you need to consume pending whitespace from input, use std::ws or switch to std::getline. Also prefer <cmath> and the std:: math functions to ensure correct overloads are found.

Example patterns that avoid the issue and add basic error checking:

#include <iostream>
#include <cmath>

int main() {
    int n;
    std::cout << "Enter N: " << std::flush;
    if (!(std::cin >> n)) { std::cerr << "Invalid integer\n"; return 1; }

    double x;
    std::cout << "Enter a value: ";
    if (!(std::cin >> x)) { std::cerr << "Invalid number\n"; return 1; }

    std::cout << "cos(x) = " << std::cos(x) << '\n';
}

Linker errors like undefined reference to std::cout or std::cin typically mean you compiled or linked as C instead of C++. Use the C++ driver so libstdc++ is linked automatically:

# Correct
g++ -std=c++17 -O2 main.cpp

# Common mistake (links as C, misses libstdc++)
gcc main.cpp

With <cmath> and std::cos/std::sqrt, g++ usually does not require -lm. See cppreference on endl, ws, <cmath>, and the g++ man page noting automatic linkage of the C++ standard library (man7.org).

Recommended Answers

All 5 Replies

endl manipulator in istream? remove it.

Thanks, Prabakar.

On a somewhat related note, I also have linking problems. Such as

test.cpp:(.text+0x206): undefined reference to `std::cout'
test.cpp:(.text+0x20b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x219): undefined reference to `std::cin'
test.cpp:(.text+0x21e): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
test.cpp:(.text+0x22c): undefined reference to `operator new[](unsigned int)'

This leads me to believe that there still might be some problems related to operators (or not). I kinda figured out that I need to put -lm at the end to link <cmath> correctly. Is there anything else besides <cmath> and <iostream> that I should include to get rid of such linking problems? Or am I doing something wrong?

Much thanks.

Actually, I answered my own question. Found out that I was using gcc in the makefile rather than g++.g++ includes gcc and more.

You probably should update your usage of the libraries to look like:

#include <cstdio>
#include <cmath>
#include <iostream> 
#include <cstdlib>

using namespace std;

Yes, thats right

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.